Exemple #1
0
def safename(filename):
    """Return a filename suitable for the cache.

    Strips dangerous and common characters to create a filename we
    can use to store the cache in.
    """

    try:
        if re_url_scheme_s.match(filename):
            if isinstance(filename,bytes):
                filename = filename.decode('utf-8')
                filename = filename.encode('idna')
            else:
                filename = filename.encode('idna')
    except UnicodeError:
        pass
    if isinstance(filename,str):
        filename=filename.encode('utf-8')
    filemd5 = _md5(filename).hexdigest().encode('utf-8')
    filename = re_url_scheme.sub(b"", filename)
    filename = re_slash.sub(b",", filename)

    # limit length of filename
    if len(filename)>200:
        filename=filename[:200]
    return b",".join((filename, filemd5)).decode('utf-8')
def md5s(data):
    ''' Return MD5 hex digest of data. '''

    buf = StringIO.StringIO(data)
    digest = _md5()
    digest.update(buf.read())
    return digest.hexdigest()
Exemple #3
0
 def _submit(self, operationUrl, data):
     # type: (str, dict) -> dict
     
     orderedData = _OrderedDict()
     isBatch = "batch" in operationUrl
     
     if not self.submitRequests and "format" in data.keys():
         data.pop("format")
     
     for key in sorted(data.keys()):
         orderedData[key] = data[key]
     data = orderedData
     
     requestUrls = data.pop("requests") if isBatch else []
     requestAsParams = "&".join(["requests[]=" + url for url in requestUrls]) if isBatch else ""
         
     urlParams = _urlencode(data)
     urlParams += "&" + requestAsParams if isBatch else ""
     urlToSignature = operationUrl + urlParams + self.privateKey
     signature = _md5(urlToSignature.encode()).hexdigest()
     finalUrl = operationUrl + urlParams + "&signature=" + signature
     
     if self.submitRequests:
         if _DEBUG: print("Requesting URL:", finalUrl)
         response = _urlopen(finalUrl).read().decode()
         
         if self.responseFormat == "json":
             return _literal_eval(response)["response"]
         else:
             return response
     else:
         if _DEBUG: print("Generated URL:", finalUrl)
         return finalUrl
Exemple #4
0
def md5(fp, offset=0, samplesize=None, chunksize=2**8):
    """
    Specialized md5 function. Just returns a hexdigest.

    Reads $chunksize bytes from the file at $fp at a time.

    If $samplesize is not None, only read that many bytes then stop, even if we haven't
    hit the end of the file.
    """
    if samplesize is not None:
        if chunksize > samplesize:
            chunksize = samplesize
    h = _md5()
    with open(fp, 'rb') as f:
        if offset:
            f.seek(offset)
        chunk = f.read(chunksize)
        bytes_read = len(chunk)
        while chunk:
            h.update(chunk)
            if samplesize is not None:
                if bytes_read == samplesize:
                    break
                elif bytes_read + chunksize > samplesize:
                    chunksize = samplesize - bytes_read
                else:
                    # Leaving this here in case I need it for debugging
                    # again later.
                    pass
            chunk = f.read(chunksize)
            bytes_read = bytes_read + chunksize
    return h.hexdigest()
Exemple #5
0
def hash_values(values, seed=None, algorithm=_md5):
    h = _md5()
    if seed is not None:
        h.update((u'%s\xff' % seed).encode('utf-8'))
    for value in values:
        hash_value(h, value)
    return h.hexdigest()
Exemple #6
0
def safename(filename):
    """Return a filename suitable for the cache.

    Strips dangerous and common characters to create a filename we
    can use to store the cache in.
    """

    try:
        if re_url_scheme_s.match(filename):
            if isinstance(filename,bytes):
                filename = filename.decode('utf-8')
                filename = filename.encode('idna')
            else:
                filename = filename.encode('idna')
    except UnicodeError:
        pass
    if isinstance(filename,str):
        filename=filename.encode('utf-8')
    filemd5 = _md5(filename).hexdigest().encode('utf-8')
    filename = re_url_scheme.sub(b"", filename)
    filename = re_slash.sub(b",", filename)

    # limit length of filename
    if len(filename)>200:
        filename=filename[:200]
    return b",".join((filename, filemd5)).decode('utf-8')
Exemple #7
0
def _hash_refnames(refnames):
    "Hashes an iterable of strings of reference names using MD5."
    hasher = _md5()
    for refname in refnames:
        hasher.update(refname.encode().rstrip())

    return hasher.digest()
Exemple #8
0
def sign_file(file_path):
    if os.path.isfile(file_path):
        f = open(file_path, 'rb')
        content = f.read()
        f.close()
        return _md5(content).hexdigest()
    return ''
Exemple #9
0
def md5s(data):
    ''' Return MD5 hex digest of data. '''

    buf = StringIO.StringIO(data)
    digest = _md5()
    digest.update(buf.read())
    return digest.hexdigest()
Exemple #10
0
def hash_values(values, seed=None, algorithm=_md5):
    h = _md5()
    if seed is not None:
        h.update((u'%s\xff' % seed).encode('utf-8'))
    for value in values:
        hash_value(h, value)
    return h.hexdigest()
    def hash(self):
        """meta hash of object ids"""

        m = _md5()
        for da in self.dot_arrays:
            m.update(da.hash.encode("UTF-8"))
        return m.hexdigest()
Exemple #12
0
        def new_server_nonce(algorithm, tokens, affordances):
            try:
                state_digest = tokens['state_digest']
            except KeyError:
                request_timestamp = \
                    algorithm.current_request_timestamp(tokens=tokens)
                try:
                    etag = algorithm.current_request_etag(tokens=tokens)
                except _bedframe_exc.AvoidingAuth:
                    etag = _b64encode(algorithm.current_request_loc)

                try:
                    private_key = tokens['private_key']
                except KeyError:
                    private_key = \
                        algorithm.private_key(tokens=tokens,
                                              affordances=affordances)

                state_digest = \
                    _md5('{}:{}:{}'.format(request_timestamp, etag,
                                           private_key))\
                     .hexdigest()

            nonce = _b64encode('{}{}'.format(request_timestamp, state_digest))
            algorithm.server_nonce_info[nonce] = \
                algorithm.ServerNonceInfo(value=nonce,
                                          loc=algorithm.current_request_loc,
                                          etag=etag,
                                          timestamp=request_timestamp,
                                          use_count=0)
            return nonce
def md5(path, buff=1024 * 1000 * 8):
    hasher = _md5()
    with open(path, 'rb') as f:
        data = f.read(buff)
        while data:
            hasher.update(data)
            data = f.read(buff)
    return hasher.hexdigest()
Exemple #14
0
def md5(s):
    try:
        from hashlib import md5 as _md5
    except ImportError:
        from md5 import md5 as _md5
    global md5
    md5 = _md5
    return _md5(s)
def md5(s):
    try:
        from hashlib import md5 as _md5
    except ImportError:
        from md5 import md5 as _md5
    global md5
    md5 = _md5
    return _md5(s)
Exemple #16
0
def md5s(data):
    """ Return MD5 hex digest of data. """

    digest = _md5()
    try:
        digest.update(data)
    except UnicodeEncodeError:
        digest.update(data.encode("utf-8"))
    return digest.hexdigest()
Exemple #17
0
        def _process_tokens(self, input, affordances):

            tokens = input.copy()

            missing_required_tokens = \
                [name for name in self.algorithm.TOKENS_NO_QOP
                 if name not in tokens]
            if missing_required_tokens:
                raise _exc.MissingTokens(missing_required_tokens)

            tokens['realm'] = iter(affordances.realms).next()

            if 'qop' in tokens:
                qop = tokens['qop']
                if qop.startswith('auth'):
                    missing_required_tokens = \
                        [name
                         for name in self.algorithm.TOKENS_QOP_AUTH_SPECIFIC
                         if name not in tokens]
                    if missing_required_tokens:
                        raise _exc.MissingTokens\
                               (missing_required_tokens,
                                'required due to token qop={}'.format(qop))

                    _lang.require_isinstance(tokens['server_nonce_use_count'],
                                             _lang.hex_intlike)

                    if qop == 'auth-int':
                        missing_required_tokens = \
                            [name
                             for name
                             in self.algorithm.TOKENS_QOP_AUTH_INT_SPECIFIC
                             if name not in tokens]
                        if missing_required_tokens:
                            raise _exc.MissingTokens\
                                   (missing_required_tokens,
                                    'required due to token qop={}'.format(qop))

                        tokens['request_entity_hash'] = \
                            _md5(self.service.current_request.body).hexdigest()

                    else:
                        if self._affordances_require_qop_auth_int_output\
                               (upstream_affordances=affordances):
                            # FIXME
                            raise ValueError
                else:
                    raise ValueError('invalid qop value {!r}: expecting one of'
                                      ' {}'
                                      .format(qop, ('auth', 'auth-int')))
            else:
                if self._affordances_require_qop_auth_output\
                       (upstream_affordances=affordances):
                    # FIXME
                    raise ValueError

            return _info.RequestAuthInfo(tokens=tokens)
Exemple #18
0
def md5s(data):
    ''' Return MD5 hex digest of data. '''

    digest = _md5()
    try:
        digest.update(data)
    except UnicodeEncodeError:
        digest.update(data.encode('utf-8'))
    return digest.hexdigest()
Exemple #19
0
def md5s(data):
    ''' Return MD5 hex digest of data. '''

    digest = _md5()
    try:
        digest.update(data)
    except UnicodeEncodeError:
        digest.update(data.encode('utf-8'))
    return digest.hexdigest()
Exemple #20
0
def md5(token):
    """
    Returns an md5 hash of a token passed as a string, performing an internal 
    conversion of the token to bytes if run in Python 3
    """
    new_token = token
    if sys.version_info[0] == 3:
        new_token = token.encode('utf-8')
    return _md5(new_token)
Exemple #21
0
    def encrypt(self, user_pwd, owner_pwd=None, use_128bit=True):
        """Encrypt this PDF file with the PDF Standard encryption handler.

        user_pwd - The "user password", which allows for opening and reading
                the PDF file with the restrictions provided.
        owner_pwd - The "owner password", which allows for opening the PDF
                files without any restrictions.  By default, the owner password is the
                same as the user password.
        use_128bit - Boolean argument as to whether to use 128bit
                encryption.  When false, 40bit encryption will be used.  By default, this
                flag is on."""
        if owner_pwd is None:
            owner_pwd = user_pwd
        if use_128bit:
            v = 2
            rev = 3
            keylen = 128 / 8
        else:
            v = 1
            rev = 2
            keylen = 40 / 8
        # permit everything:
        p = -1
        o = ByteStringObject(_u.algorithm_33(owner_pwd, user_pwd, rev, keylen))
        id_1 = _md5(bytes(repr(time.time()), _u.ENCODING_UTF8)).digest()
        id_2 = _md5(bytes(repr(random.random()), _u.ENCODING_UTF8)).digest()
        self._id = ArrayObject((ByteStringObject(id_1), ByteStringObject(id_2)))
        if rev == 2:
            u, key = _u.algorithm_34(user_pwd, o, p, id_1)
        else:
            assert rev == 3
            u, key = _u.algorithm_35(user_pwd, rev, keylen, o, p, id_1, False)
        encrypt = DictObject()
        encrypt[NameObject(b'/Filter')] = NameObject(b'/Standard')
        encrypt[NameObject(b'/V')] = NumberObject(v)
        if v == 2:
            encrypt[NameObject(b'/Length')] = NumberObject(keylen * 8)
        encrypt[NameObject(b'/R')] = NumberObject(rev)
        encrypt[NameObject(b'/O')] = ByteStringObject(o)
        encrypt[NameObject(b'/U')] = ByteStringObject(u)
        encrypt[NameObject(b'/P')] = NumberObject(p)
        self._encrypt = self._add_object(encrypt)
        self._encrypt_key = key
Exemple #22
0
 def fingerprint(self):
     """Return the fingerprint of this key - this is useful to help
        work out which key to use to decrypt data
     """
     from hashlib import md5 as _md5
     md5 = _md5()
     md5.update(self.bytes())
     h = md5.hexdigest()
     # return this signature as "AA:BB:CC:DD:EE:etc."
     return ":".join([h[i:i + 2] for i in range(0, len(h), 2)])
Exemple #23
0
def md5_hex_digest(inp, encoding='utf8') -> str:
    """Generates MD5 hex digest for string or bytes.
    """

    if isinstance(inp, str):
        inp = bytes(inp, encoding)

    m = _md5()
    m.update(inp)

    return m.hexdigest()
Exemple #24
0
    def build_path(self, key):
        "Build the path for the given key"
        keymd5 = _md5(key).hexdigest()
        
        parts = []
        for schema in self.path_schema:
            parts += [keymd5[schema[0]:schema[1]]]

        parts += [self.safe(key)]

        return os.path.join(*parts)
Exemple #25
0
def md5(text: str) -> str:
    """
    计算字符串的 MD5 值

    :param text: 待计算的文本
    :return: md5 字符串

    >>> md5("从零开始的异世界")
    '6ce5c6d9c3c445e8ad93bcbb7453e590'
    """
    return _md5(text.encode("utf-8")).hexdigest()
Exemple #26
0
def algorithm_32(password, rev, keylen, owner_entry, p_entry, id1_entry, metadata_encrypt=True):
    """Implementation of algorithm 3.2 of the PDF standard security handler,
    section 3.5.2 of the PDF 1.6 reference."""
    # 1. Pad or truncate the password string to exactly 32 bytes.  If the
    # password string is more than 32 bytes long, use only its first 32 bytes;
    # if it is less than 32 bytes long, pad it by appending the required number
    # of additional bytes from the beginning of the padding string
    # (_encryption_padding).
    password = (password + _encryption_padding)[:32]
    # 2. Initialize the MD5 hash function and pass the result of step 1 as
    # input to this function.
    m = _md5(password)
    # 3. Pass the value of the encryption dictionary's /O entry to the MD5 hash
    # function.
    m.update(owner_entry)
    # 4. Treat the value of the /P entry as an unsigned 4-byte integer and pass
    # these bytes to the MD5 hash function, low-order byte first.
    p_entry = _s.pack('<i', p_entry)
    m.update(p_entry)
    # 5. Pass the first element of the file's file identifier array to the MD5
    # hash function.
    m.update(id1_entry)
    # 6. (Revision 3 or greater) If document metadata is not being encrypted,
    # pass 4 bytes with the value 0xFFFFFFFF to the MD5 hash function.
    if rev >= 3 and not metadata_encrypt:
        m.update(b'\xff\xff\xff\xff')
    # 7. Finish the hash.
    md5_hash = m.digest()
    # 8. (Revision 3 or greater) Do the following 50 times: Take the output
    # from the previous MD5 hash and pass the first n bytes of the output as
    # input into a new MD5 hash, where n is the number of bytes of the
    # encryption key as defined by the value of the encryption dictionary's
    # /Length entry.
    if rev >= 3:
        for i in range(50):
            md5_hash = _md5(md5_hash[:keylen]).digest()
    # 9. Set the encryption key to the first n bytes of the output from the
    # final MD5 hash, where n is always 5 for revision 2 but, for revision 3 or
    # greater, depends on the value of the encryption dictionary's /Length
    # entry.
    return md5_hash[:keylen]
Exemple #27
0
def algorithm_33_1(password, rev, keylen):
    """Steps 1-4 of algorithm 3.3"""
    # 1. Pad or truncate the owner password string as described in step 1 of
    # algorithm 3.2.  If there is no owner password, use the user password
    # instead.
    password = (password + _encryption_padding)[:32]
    # 2. Initialize the MD5 hash function and pass the result of step 1 as
    # input to this function.
    m = _md5(password)
    # 3. (Revision 3 or greater) Do the following 50 times: Take the output
    # from the previous MD5 hash and pass it as input into a new MD5 hash.
    md5_hash = m.digest()
    if rev >= 3:
        for i in range(50):
            md5_hash = _md5(md5_hash).digest()
    # 4. Create an RC4 encryption key using the first n bytes of the output
    # from the final MD5 hash, where n is always 5 for revision 2 but, for
    # revision 3 or greater, depends on the value of the encryption
    # dictionary's /Length entry.
    key = md5_hash[:keylen]
    return key
Exemple #28
0
 def get(self, md5):
     (file_version, file_name, file_md5,
      file_data) = manager.get_image_data(md5)
     response = make_response(
         send_file(BytesIO(file_data),
                   as_attachment=True,
                   attachment_filename=file_name))
     # MD5 has to include the signature for verification here
     # So we're not using file_md5 (which is excluding the signature)
     response.headers['X-MD5'] = _md5(file_data).hexdigest()
     response.headers['Content-Length'] = len(file_data)
     return response
Exemple #29
0
def md5(filename):
    ''' Return MD5 hex digest of local file, or None if file is not present. '''
    if not os.path.exists(filename):
        return None
    digest = _md5()
    blocksize = 64 * 1024
    infile = open(filename, 'rb')
    block = infile.read(blocksize)
    while block:
        digest.update(block)
        block = infile.read(blocksize)
    infile.close()
    return digest.hexdigest()
Exemple #30
0
def md5(filename):
    ''' Return MD5 hex digest of local file, or None if file is not present. '''
    if not os.path.exists(filename):
        return None
    digest = _md5()
    blocksize = 64 * 1024
    infile = open(filename, 'rb')
    block = infile.read(blocksize)
    while block:
        digest.update(block)
        block = infile.read(blocksize)
    infile.close()
    return digest.hexdigest()
Exemple #31
0
def get_size_and_checksum(data):
    """Calculates the size and md5 of the passed data

       Args:
            data (byte): data to calculate checksum for
        Returns:
            tuple (int,str): size of data and its md5 hash
    """
    from hashlib import md5 as _md5
    md5 = _md5()
    md5.update(data)

    return (len(data), str(md5.hexdigest()))
Exemple #32
0
    def md5(data):
        """Return the MD5 checksum of the passed data"""
        if data is None:
            return None

        from hashlib import md5 as _md5

        if isinstance(data, str):
            data = data.encode("utf-8")

        md5 = _md5()
        md5.update(data)
        return md5.hexdigest()
Exemple #33
0
 def md5(self, filename):
     ''' Return MD5 hex digest of local file, or None if file is not present. '''
     if not os.path.exists(filename):
         return None
     if os.path.isdir(filename):
         self.fail_json(msg="attempted to take md5sum of directory: %s" % filename)
     digest = _md5()
     blocksize = 64 * 1024
     infile = open(filename, 'rb')
     block = infile.read(blocksize)
     while block:
         digest.update(block)
         block = infile.read(blocksize)
     infile.close()
     return digest.hexdigest()
 def md5(self, filename):
     ''' Return MD5 hex digest of local file, or None if file is not present. '''
     if not os.path.exists(filename):
         return None
     if os.path.isdir(filename):
         self.fail_json(msg="attempted to take md5sum of directory: %s" % filename)
     digest = _md5()
     blocksize = 64 * 1024
     infile = open(filename, 'rb')
     block = infile.read(blocksize)
     while block:
         digest.update(block)
         block = infile.read(blocksize)
     infile.close()
     return digest.hexdigest()
Exemple #35
0
def md5(filename):
    ''' Return MD5 hex digest of local file, None if file is not present or a directory. '''

    if not os.path.exists(filename) or os.path.isdir(filename):
        return None
    digest = _md5()
    blocksize = 64 * 1024
    try:
        infile = open(filename, 'rb')
        block = infile.read(blocksize)
        while block:
            digest.update(block)
            block = infile.read(blocksize)
        infile.close()
    except IOError, e:
        raise errors.AnsibleError("error while accessing the file %s, error was: %s" % (filename, e))
Exemple #36
0
def md5(filename):
    ''' Return MD5 hex digest of local file, or None if file is not present. '''

    if not os.path.exists(filename):
        return None
    digest = _md5()
    blocksize = 64 * 1024
    try:
        infile = open(filename, 'rb')
        block = infile.read(blocksize)
        while block:
            digest.update(block)
            block = infile.read(blocksize)
        infile.close()
    except IOError, e:
        raise errors.AnsibleError("error while accessing the file %s, error was: %s" % (filename, e))
Exemple #37
0
def _uuid(*args):
    """
    uuid courtesy of Carl Free Jr:
    (http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/213761)
    """

    t = long(time.time() * 1000)
    r = long(random.random() * 100000000000000000L)

    try:
        a = socket.gethostbyname(socket.gethostname())
    except:
        # if we can't get a network address, just imagine one
        a = random.random() * 100000000000000000L
    data = str(t) + " " + str(r) + " " + str(a) + " " + str(args)
    data = _md5(data).hexdigest()

    return data
Exemple #38
0
    def checksum(data):
        """Return the checksum of the passed data. This is used either
           for validating data, and is also used to create a checksum
           of the URL so that the user can demonstrate that they can
           decrypt this OSPar

           Args:
                data (str): Data to checksum
           Returns:
                str: MD5 checksum of data
        """
        from hashlib import md5 as _md5
        md5 = _md5()

        if isinstance(data, str):
            data = data.encode("utf-8")

        md5.update(data)
        return md5.hexdigest()
def _get_filesize_and_checksum(filename):
    """Calculates the size in bytes of the file and
        the MD5 hash

       Args:
            filename (str): name of file to calculate hash for
        Returns:
            tuple (int, str): size of file in bytes, md5 hash of file
    """
    from hashlib import md5 as _md5
    md5 = _md5()
    size = 0

    with open(filename, "rb") as f:
        for chunk in iter(lambda: f.read(4096), b""):
            md5.update(chunk)
            size += len(chunk)

    return (size, str(md5.hexdigest()))
Exemple #40
0
    def xmlfilename_for_run(self, run, parent=None, zip=False):
        """
        Return unique filename for requested run - no check for valid
        path is performed

        If Parent is provided, result is path relative to parent
        """

        md5 = _md5()
        key = f"{self.data_dir}/{self.prefix}{run}/{self.pattern}"
        md5.update(key.encode())

        xmlfilename = _Path('%d-%s.xml' % (run, md5.hexdigest()))
        if zip:
            xmlfilename = xmlfilename.with_suffix('.xml.gz')

        if parent is not None:
            xmlfilename = _Path(parent) / xmlfilename
        return _Path(xmlfilename)
Exemple #41
0
def get_filesize_and_checksum(filename):
    """Opens the file with the passed filename and calculates
        its size and md5 hash

       Args:
            filename (str): filename to calculate size and checksum for
        Returns:
            tuple (int,str): size of data and its md5 hash

    """

    from hashlib import md5 as _md5
    md5 = _md5()
    size = 0

    with open(filename, "rb") as f:
        for chunk in iter(lambda: f.read(4096), b""):
            md5.update(chunk)
            size += len(chunk)

    return (size, str(md5.hexdigest()))
Exemple #42
0
 def request(self, method, request_uri, headers, content, cnonce = None):
     """Modify the request headers"""
     H = lambda x: _md5(x.encode('utf-8')).hexdigest()
     KD = lambda s, d: H("%s:%s" % (s, d))
     A2 = "".join([method, ":", request_uri])
     self.challenge['cnonce'] = cnonce or _cnonce() 
     request_digest  = '"%s"' % KD(H(self.A1), "%s:%s:%s:%s:%s" % (self.challenge['nonce'], 
                 '%08x' % self.challenge['nc'], 
                 self.challenge['cnonce'], 
                 self.challenge['qop'], H(A2)
                 )) 
     headers['Authorization'] = 'Digest username="******", realm="%s", nonce="%s", uri="%s", algorithm=%s, response=%s, qop=%s, nc=%08x, cnonce="%s"' % (
             self.credentials[0], 
             self.challenge['realm'],
             self.challenge['nonce'],
             request_uri, 
             self.challenge['algorithm'],
             request_digest,
             self.challenge['qop'],
             self.challenge['nc'],
             self.challenge['cnonce'],
             )
     self.challenge['nc'] += 1
Exemple #43
0
 def request(self, method, request_uri, headers, content, cnonce = None):
     """Modify the request headers"""
     H = lambda x: _md5(x.encode('utf-8')).hexdigest()
     KD = lambda s, d: H("%s:%s" % (s, d))
     A2 = "".join([method, ":", request_uri])
     self.challenge['cnonce'] = cnonce or _cnonce() 
     request_digest  = '"%s"' % KD(H(self.A1), "%s:%s:%s:%s:%s" % (self.challenge['nonce'], 
                 '%08x' % self.challenge['nc'], 
                 self.challenge['cnonce'], 
                 self.challenge['qop'], H(A2)
                 )) 
     headers['Authorization'] = 'Digest username="******", realm="%s", nonce="%s", uri="%s", algorithm=%s, response=%s, qop=%s, nc=%08x, cnonce="%s"' % (
             self.credentials[0], 
             self.challenge['realm'],
             self.challenge['nonce'],
             request_uri, 
             self.challenge['algorithm'],
             request_digest,
             self.challenge['qop'],
             self.challenge['nc'],
             self.challenge['cnonce'],
             )
     self.challenge['nc'] += 1
Exemple #44
0
def algorithm_35(password, rev, keylen, owner_entry, p_entry, id1_entry, _metadata_encrypt):
    """Implementation of algorithm 3.4 of the PDF standard security handler,
    section 3.5.2 of the PDF 1.6 reference."""
    # 1. Create an encryption key based on the user password string, as
    # described in Algorithm 3.2.
    key = algorithm_32(password, rev, keylen, owner_entry, p_entry, id1_entry)
    # 2. Initialize the MD5 hash function and pass the 32-byte padding string
    # shown in step 1 of Algorithm 3.2 as input to this function. 
    m = _md5()
    m.update(bytes(_encryption_padding, ENCODING_UTF8))
    # 3. Pass the first element of the file's file identifier array (the value
    # of the ID entry in the document's trailer dictionary; see Table 3.13 on
    # page 73) to the hash function and finish the hash.  (See implementation
    # note 25 in Appendix H.) 
    m.update(id1_entry)
    md5_hash = m.digest()
    # 4. Encrypt the 16-byte result of the hash, using an RC4 encryption
    # function with the encryption key from step 1. 
    val = rc4_encrypt(key, md5_hash)
    # 5. Do the following 19 times: Take the output from the previous
    # invocation of the RC4 function and pass it as input to a new invocation
    # of the function; use an encryption key generated by taking each byte of
    # the original encryption key (obtained in step 2) and performing an XOR
    # operation between that byte and the single-byte value of the iteration
    # counter (from 1 to 19). 
    for _i in range(1, 20):
        new_key = ''
        for j in range(len(key)):
            new_key += chr(ord(key[j:j + 1]) ^ _i)
        val = rc4_encrypt(new_key, val)
    # 6. Append 16 bytes of arbitrary padding to the output from the final
    # invocation of the RC4 function and store the 32-byte result as the value
    # of the U entry in the encryption dictionary. 
    # (author note: I don't know what "arbitrary padding" is supposed to
    # mean, so I have used null bytes.  This seems to match a few other
    # people's implementations)
    return val + ('\x00' * 16), key
Exemple #45
0
def _add_single_bbox_on_image(
    image,
    left,
    top,
    right,
    bottom,
    label=None,
    color=None,
    font_size=100,
    box_line_width=15,
):
    """ Add single bounding box with label on a given image.

    Add single bounding box and a label text with label on a given image. If the
    label text exceeds the original image border, it would be cropped.
    """
    try:
        left, top, right, bottom = int(left), int(top), int(right), int(bottom)
    except ValueError:
        raise TypeError("'left', 'top', 'right' & 'bottom' must be a number")

    if label and not color:
        hex_digest = _md5(label.encode()).hexdigest()
        color_index = int(hex_digest, 16) % len(_COLOR_NAME_TO_RGB)
        color = _COLOR_NAMES[color_index]
    elif not label:
        color = random.choice(_COLOR_NAMES)

    colors = [list(item) for item in _COLOR_NAME_TO_RGB[color]]
    color, color_text = colors

    _cv2.rectangle(image, (left, top), (right, bottom), color, box_line_width)

    if label:
        label_image = _get_label_image(label, color_text, color, font_size)
        _add_label_on_image(label_image, image, left, top, color)
Exemple #46
0
def md5s(data):
    ''' Return MD5 hex digest of data. '''

    digest = _md5()
    digest.update(data.encode('utf-8'))
    return digest.hexdigest()
Exemple #47
0
def _cnonce():
    dig = _md5(("%s:%s" % (time.ctime(), ["0123456789"[random.randrange(0, 9)] for i in range(20)])).encode('utf-8')).hexdigest()
    return dig[:16]
Exemple #48
0
def md5(s):
    if isinstance(s, unicode):
        s = s.encode('utf-8')
    return _md5(s).hexdigest()
Exemple #49
0
def md5s(data):
    """ Return MD5 hex digest of data. """

    digest = _md5()
    digest.update(data)
    return digest.hexdigest()
Exemple #50
0
def md5s(data):
    ''' Return MD5 hex digest of data. '''

    digest = _md5()
    digest.update(data)
    return digest.hexdigest()
Exemple #51
0
 def md5(self, filename):
     ''' Return MD5 hex digest of local file using digest_from_file(). '''
     return self.digest_from_file(filename, _md5())
def md5s(data):
    ''' Return MD5 hex digest of data. '''

    digest = _md5()
    digest.update(data)
    return digest.hexdigest()
Exemple #53
0
 def get_resource_key(self, url):
     return base64.urlsafe_b64encode(_md5(url).digest())
Exemple #54
0
 def _encode_pwd(self):
     from hashlib import md5 as _md5
     md5 = lambda x: _md5(x).hexdigest().upper()
     self.encoded_pwd = md5(
         md5(self._hex2chr(md5(self.pwd)) + self.uin) + self.vc.upper())
Exemple #55
0
# coding=utf-8

from base import BaseSB
from hashlib import md5 as _md5

md5 = lambda x: _md5(str(x)).hexdigest()


class SB1(BaseSB):
    """
    完成了全部发送消息的函数
    关于接收部分,每个人需求不同,自己使用装饰器定义函数.
    """

    def login(self, username, password, clientversion="1", clientname="Client"):
        self.send(
            type=201, data="%s %s %s %s" % (username, md5(md5(username) + md5(password)), clientversion, clientname)
        )

    def alive(self):
        self.send(type=20)

    def getuserlist(self):
        self.send(type=300)
        data = self.recv()
        print data
        return data[4].split(",")

    def sendmessage(self, to, message):
        self.send(type=100, data="%s %s" % (to, message))
Exemple #56
0
"""
sentry.utils.hashlib
~~~~~~~~~~~~~~~~~~~~

:copyright: (c) 2010-2015 by the Sentry Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""
from __future__ import absolute_import

from hashlib import md5 as _md5
from hashlib import sha1 as _sha1

from django.utils.encoding import force_bytes


md5_text = lambda x: _md5(force_bytes(x, errors='replace'))
sha1_text = lambda x: _sha1(force_bytes(x, errors='replace'))
Exemple #57
0
def md5(*bits):
    return _md5(':'.join((force_bytes(bit, errors='replace') for bit in bits)))
Exemple #58
0
def md5_text(*args):
    m = _md5()
    for x in args:
        m.update(force_bytes(x, errors='replace'))
    return m
Exemple #59
0
def set_http_cache(dir):
    try:
        cache = httplib2.FileCache(dir, safe=lambda x: _md5(x).hexdigest())
        http.cache = cache
    except:
        pass
Exemple #60
0
 def getAuthKey(self):
     # Use first AUTH_KEY_LEN characters of md5 hash of SECRET_KEY
     return _md5(settings.SECRET_KEY).hexdigest()[:self.AUTH_KEY_LEN]