Ejemplo n.º 1
0
def parse_mc3(hash,
              prefix,
              sep=_UDOLLAR,
              rounds_base=10,
              default_rounds=None,
              handler=None):
    hash = to_unicode(hash, 'ascii', 'hash')
    if not hash.startswith(prefix):
        raise exc.InvalidHashError(handler)
    parts = hash[len(prefix):].split(sep)
    if len(parts) == 3:
        rounds, salt, chk = parts
    else:
        if len(parts) == 2:
            rounds, salt = parts
            chk = None
        else:
            raise exc.MalformedHashError(handler)
    if rounds.startswith(_UZERO) and rounds != _UZERO:
        raise exc.ZeroPaddedRoundsError(handler)
    else:
        if rounds:
            rounds = int(rounds, rounds_base)
        else:
            if default_rounds is None:
                raise exc.MalformedHashError(handler, 'empty rounds field')
            else:
                rounds = default_rounds
    return (rounds, salt, chk or None)
Ejemplo n.º 2
0
def parse_int(source, base=10, default=None, param='value', handler=None):
    if source.startswith(_UZERO) and source != _UZERO:
        raise exc.MalformedHashError(handler, 'zero-padded %s field' % param)
    else:
        if source:
            return int(source, base)
        if default is None:
            raise exc.MalformedHashError(handler, 'empty %s field' % param)
        else:
            return default
    return
Ejemplo n.º 3
0
def parse_mc2(hash, prefix, sep=_UDOLLAR, handler=None):
    hash = to_unicode(hash, 'ascii', 'hash')
    if not hash.startswith(prefix):
        raise exc.InvalidHashError(handler)
    parts = hash[len(prefix):].split(sep)
    if len(parts) == 2:
        salt, chk = parts
        return (salt, chk or None)
    if len(parts) == 1:
        return (parts[0], None)
    raise exc.MalformedHashError(handler)
    return
Ejemplo n.º 4
0
 def _adapt_backend_error(cls, err, hash=None, self=None):
     backend = cls.get_backend()
     if self is None and hash is not None:
         self = cls.from_string(hash)
     if self is not None:
         self._validate_constraints(self.memory_cost, self.parallelism)
         if backend == 'argon2_cffi' and self.data is not None:
             raise NotImplementedError(
                 "argon2_cffi backend doesn't support the 'data' parameter")
     text = str(err)
     if text not in ('Decoding failed', ):
         reason = '%s reported: %s: hash=%r' % (backend, text, hash)
     else:
         reason = repr(hash)
     raise exc.MalformedHashError(cls, reason=reason)
     return
Ejemplo n.º 5
0
 def from_string(cls, hash):
     if isinstance(hash, unicode):
         hash = hash.encode('utf-8')
     if not isinstance(hash, bytes):
         raise exc.ExpectedStringError(hash, 'hash')
     m = cls._hash_regex.match(hash)
     if not m:
         raise exc.MalformedHashError(cls)
     type, version, memory_cost, time_cost, parallelism, keyid, data, salt, digest = m.group(
         'type', 'version', 'memory_cost', 'time_cost', 'parallelism',
         'keyid', 'data', 'salt', 'digest')
     if keyid:
         raise NotImplementedError("argon2 'keyid' parameter not supported")
     return cls(type_d=type == 'd',
                version=int(version) if version else 16,
                memory_cost=int(memory_cost),
                rounds=int(time_cost),
                parallelism=int(parallelism),
                salt=b64s_decode(salt) if salt else None,
                data=b64s_decode(data) if data else None,
                checksum=b64s_decode(digest) if digest else None)