def from_string(cls, hash): hash = to_unicode(hash, "ascii", "hash") ident, data = hash[0:3], hash[3:] if ident != cls.ident: raise exc.InvalidHashError() rounds, salt, chk = data[0], data[1:9], data[9:] return cls( rounds=h64.decode_int6(rounds.encode("ascii")), salt=salt, checksum=chk or None, )
def from_string(cls, hash, **context): # default from_string() which strips optional prefix, # and passes rest unchanged as checksum value. hash = to_unicode(hash, "ascii", "hash") hash = cls._norm_hash(hash) # could enable this for extra strictness ##pat = cls._hash_regex ##if pat and pat.match(hash) is None: ## raise ValueError("not a valid %s hash" % (cls.name,)) prefix = cls._hash_prefix if prefix: if hash.startswith(prefix): hash = hash[len(prefix):] else: raise exc.InvalidHashError(cls) # Decode the base64 stored actual hash hash = unicode(base64.b64decode(hash)) return cls(checksum=hash, **context)