Ejemplo n.º 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)
Ejemplo n.º 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)
         ]))
Ejemplo n.º 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)
Ejemplo n.º 4
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))
Ejemplo n.º 5
0
    def to_string(self):
        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 "$argon2%s$%sm=%d,t=%d,p=%d%s$%s$%s" % (
            uascii_to_str(self.type),
            vstr, 
            self.memory_cost,
            self.rounds, 
            self.parallelism,
            kdstr,
            bascii_to_str(b64s_encode(self.salt)),
            bascii_to_str(b64s_encode(self.checksum)),
        )
Ejemplo n.º 6
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)
Ejemplo n.º 7
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)
     # 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=cls._get_backend_type(self.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
Ejemplo n.º 8
0
def hexstr(data):
    """return bytes as hex str"""
    return bascii_to_str(hexlify(data))
Ejemplo n.º 9
0
 def hexdigest(self):
     return bascii_to_str(hexlify(self.digest()))
Ejemplo n.º 10
0
 def test_md4_digest(self):
     """test md4 digest()"""
     from lib.passlib.utils.md4 import md4
     for input, hex in self.vectors:
         out = bascii_to_str(hexlify(md4(input).digest()))
         self.assertEqual(out, hex)
Ejemplo n.º 11
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)
Ejemplo n.º 12
0
 def to_string(self):
     raw = self.salt + self.checksum
     # raw bytes format - BIDENT2 + raw
     return "0x0100" + bascii_to_str(hexlify(raw)).upper()
Ejemplo n.º 13
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()