Example #1
0
 def to_string(self):
     salt = bascii_to_str(ab64_encode(self.salt))
     chkmap = self.checksum
     chk_str = ",".join("%s=%s" %
                        (alg, bascii_to_str(ab64_encode(chkmap[alg])))
                        for alg in self.algs)
     return '$scram$%d$%s$%s' % (self.rounds, salt, chk_str)
Example #2
0
 def to_string(self):
     ident = self.ident
     if ident == IDENT_SCRYPT:
         return "$scrypt$ln=%d,r=%d,p=%d$%s$%s" % (
             self.rounds,
             self.block_size,
             self.parallelism,
             bascii_to_str(b64s_encode(self.salt)),
             bascii_to_str(b64s_encode(self.checksum)),
         )
     else:
         assert ident == IDENT_7
         salt = self.salt
         try:
             salt.decode("ascii")
         except UnicodeDecodeError:
             raise suppress_cause(
                 NotImplementedError(
                     "scrypt $7$ hashes dont support non-ascii salts"))
         return bascii_to_str(b"".join([
             b"$7$",
             h64.encode_int6(self.rounds),
             h64.encode_int30(self.block_size),
             h64.encode_int30(self.parallelism), self.salt, b"$",
             h64.encode_bytes(self.checksum)
         ]))
Example #3
0
 def to_string(self, withchk=True):
     salt = bascii_to_str(ab64_encode(self.salt))
     chkmap = self.checksum
     if withchk and chkmap:
         chk_str = ",".join("%s=%s" % (alg, bascii_to_str(ab64_encode(chkmap[alg]))) for alg in self.algs)
     else:
         chk_str = ",".join(self.algs)
     return "$scram$%d$%s$%s" % (self.rounds, salt, chk_str)
Example #4
0
 def to_string(self):
     salt = bascii_to_str(ab64_encode(self.salt))
     chkmap = self.checksum
     chk_str = ",".join(
         "%s=%s" % (alg, bascii_to_str(ab64_encode(chkmap[alg])))
         for alg in self.algs
     )
     return '$scram$%d$%s$%s' % (self.rounds, salt, chk_str)
Example #5
0
 def genhash(cls, secret, config):
     # TODO: add in 'encoding' support once that's finalized in 1.8 / 1.9.
     uh.validate_secret(secret)
     secret = to_bytes(secret, "utf-8")
     self = cls.from_string(config)
     if self.type_d:
         type = _argon2_cffi.low_level.Type.D
     else:
         type = _argon2_cffi.low_level.Type.I
     # XXX: doesn't seem to be a way to make this honor max_threads
     try:
         result = bascii_to_str(
             _argon2_cffi.low_level.hash_secret(
                 type=type,
                 memory_cost=self.memory_cost,
                 time_cost=self.rounds,
                 parallelism=self.parallelism,
                 salt=to_bytes(self.salt),
                 hash_len=self.checksum_size,
                 secret=secret,
                 version=self.version,
             ))
     except _argon2_cffi.exceptions.HashingError as err:
         raise cls._adapt_backend_error(err, hash=config)
     if self.version == 0x10:
         # workaround: argon2 0x13 always returns "v=" segment, even for 0x10 hashes
         result = result.replace("$v=16$", "$")
     return result
Example #6
0
 def genhash(cls, secret, config):
     # TODO: add in 'encoding' support once that's finalized in 1.8 / 1.9.
     uh.validate_secret(secret)
     secret = to_bytes(secret, "utf-8")
     self = cls.from_string(config)
     if self.type_d:
         type = _argon2_cffi.low_level.Type.D
     else:
         type = _argon2_cffi.low_level.Type.I
     # XXX: doesn't seem to be a way to make this honor max_threads
     try:
         result = bascii_to_str(_argon2_cffi.low_level.hash_secret(
             type=type,
             memory_cost=self.memory_cost,
             time_cost=self.rounds,
             parallelism=self.parallelism,
             salt=to_bytes(self.salt),
             hash_len=self.checksum_size,
             secret=secret,
             version=self.version,
         ))
     except _argon2_cffi.exceptions.HashingError as err:
         raise cls._adapt_backend_error(err, hash=config)
     if self.version == 0x10:
         # workaround: argon2 0x13 always returns "v=" segment, even for 0x10 hashes
         result = result.replace("$v=16$", "$")
     return result
    def test_md4_digest(self):
        "test md4 digest()"
        from passlib.utils.md4 import md4

        for input, hex in self.vectors:
            out = bascii_to_str(hexlify(md4(input).digest()))
            self.assertEqual(out, hex)
Example #8
0
def b32encode(source):
    """
    wrapper around :func:`base64.b32encode` which strips padding,
    and returns a native string.
    """
    # NOTE: using upper case by default here, since 'I & L' are less
    #       visually ambiguous than 'i & l'
    return bascii_to_str(_b32encode(source).rstrip(B_EQUAL))
Example #9
0
def b32encode(source):
    """
    wrapper around :func:`base64.b32encode` which strips padding,
    and returns a native string.
    """
    # NOTE: using upper case by default here, since 'I & L' are less
    #       visually ambiguous than 'i & l'
    return bascii_to_str(_b32encode(source).rstrip(B_EQUAL))
Example #10
0
 def to_string(self):
     ident = str(self.ident_values[self.type_d])
     version = self.version
     if version == 0x10:
         vstr = ""
     else:
         vstr = "v=%d$" % version
     data = self.data
     if data:
         kdstr = ",data=" + bascii_to_str(b64s_encode(self.data))
     else:
         kdstr = ""
     # NOTE: 'keyid' param currently not supported
     return "%s%sm=%d,t=%d,p=%d%s$%s$%s" % (
         ident, vstr, self.memory_cost, self.rounds, self.parallelism,
         kdstr, bascii_to_str(b64s_encode(
             self.salt)), bascii_to_str(b64s_encode(self.checksum)))
Example #11
0
 def to_string(self):
     ident = str(self.ident_values[self.type_d])
     version = self.version
     if version == 0x10:
         vstr = ""
     else:
         vstr = "v=%d$" % version
     data = self.data
     if data:
         kdstr = ",data=" + bascii_to_str(b64s_encode(self.data))
     else:
         kdstr = ""
     # NOTE: 'keyid' param currently not supported
     return "%s%sm=%d,t=%d,p=%d%s$%s$%s" % (ident, vstr, self.memory_cost,
                                            self.rounds, self.parallelism,
                                            kdstr,
                                            bascii_to_str(b64s_encode(self.salt)),
                                            bascii_to_str(b64s_encode(self.checksum)))
Example #12
0
 def hash(cls, secret):
     # TODO: add in 'encoding' support once that's finalized in 1.8 / 1.9.
     uh.validate_secret(secret)
     secret = to_bytes(secret, "utf-8")
     # XXX: doesn't seem to be a way to make this honor max_threads
     try:
         return bascii_to_str(_argon2_cffi.low_level.hash_secret(
             type=_argon2_cffi.low_level.Type.I,
             memory_cost=cls.memory_cost,
             time_cost=cls.default_rounds,
             parallelism=cls.parallelism,
             salt=to_bytes(cls._generate_salt()),
             hash_len=cls.checksum_size,
             secret=secret,
         ))
     except _argon2_cffi.exceptions.HashingError as err:
         raise cls._adapt_backend_error(err)
Example #13
0
 def hash(cls, secret):
     # TODO: add in 'encoding' support once that's finalized in 1.8 / 1.9.
     uh.validate_secret(secret)
     secret = to_bytes(secret, "utf-8")
     # XXX: doesn't seem to be a way to make this honor max_threads
     try:
         return bascii_to_str(
             _argon2_cffi.low_level.hash_secret(
                 type=cls._get_backend_type(cls.type),
                 memory_cost=cls.memory_cost,
                 time_cost=cls.default_rounds,
                 parallelism=cls.parallelism,
                 salt=to_bytes(cls._generate_salt()),
                 hash_len=cls.checksum_size,
                 secret=secret,
             ))
     except _argon2_cffi.exceptions.HashingError as err:
         raise cls._adapt_backend_error(err)
Example #14
0
 def to_string(self):
     chk = self.checksum
     salt = self.salt
     data = bascii_to_str(b64encode(salt+chk))
     return "{FSHP%d|%d|%d}%s" % (self.variant, len(salt), self.rounds, data)
Example #15
0
 def hexdigest(self):
     return bascii_to_str(hexlify(self.digest()))
 def test_md4_digest(self):
     """digest() method"""
     md4 = self.get_md4_const()
     for input, hex in self.vectors:
         out = bascii_to_str(hexlify(md4(input).digest()))
         self.assertEqual(out, hex)
Example #17
0
def hexstr(data):
    """return bytes as hex str"""
    return bascii_to_str(hexlify(data))
def hexstr(data):
    """return bytes as hex str"""
    return bascii_to_str(hexlify(data))
Example #19
0
 def to_string(self):
     raw = self.salt + (self.checksum or self._stub_checksum)
     # raw bytes format - BIDENT2 + raw
     return "0x0100" + bascii_to_str(hexlify(raw)).upper()
 def test_md4_digest(self):
     "test md4 digest()"
     from passlib.utils.md4 import md4
     for input, hex in self.vectors:
         out = bascii_to_str(hexlify(md4(input).digest()))
         self.assertEqual(out, hex)
Example #21
0
 def test_md4_digest(self):
     """digest() method"""
     md4 = self.get_md4_const()
     for input, hex in self.vectors:
         out = bascii_to_str(hexlify(md4(input).digest()))
         self.assertEqual(out, hex)
Example #22
0
 def to_string(self):
     chk = self.checksum or self._stub_checksum
     salt = self.salt
     data = bascii_to_str(b64encode(salt+chk))
     return "{FSHP%d|%d|%d}%s" % (self.variant, len(salt), self.rounds, data)
Example #23
0
 def hexdigest(self):
     return bascii_to_str(hexlify(self.digest()))
Example #24
0
 def to_string(self):
     raw = self.salt + (self.checksum or self._stub_checksum)
     # raw bytes format - BIDENT2 + raw
     return "0x0100" + bascii_to_str(hexlify(raw)).upper()