예제 #1
0
    def _calc_checksum(self, secret):
        if isinstance(secret, unicode):
            # XXX: no idea what unicode policy is, but all examples are
            # 7-bit ascii compatible, so using UTF-8
            secret = secret.encode("utf-8")

        user = self.user
        if user:
            # NOTE: not *positive* about this, but it looks like per-user
            # accounts use first 4 chars of user as salt, whereas global
            # "enable" passwords don't have any salt at all.
            if isinstance(user, unicode):
                user = user.encode("utf-8")
            secret += user[:4]

        # pad/truncate to 16
        secret = right_pad_string(secret, 16)

        # md5 digest
        hash = md5(secret).digest()

        # drop every 4th byte
        hash = join_byte_elems(c for i,c in enumerate(hash) if i & 3 < 3)

        # encode using Hash64
        return h64.encode_bytes(hash).decode("ascii")
예제 #2
0
    def _calc_checksum(self, secret):
        if isinstance(secret, unicode):
            # XXX: no idea what unicode policy is, but all examples are
            # 7-bit ascii compatible, so using UTF-8
            secret = secret.encode("utf-8")

        user = self.user
        if user:
            # not positive about this, but it looks like per-user
            # accounts use the first 4 chars of the username as the salt,
            # whereas global "enable" passwords don't have any salt at all.
            if isinstance(user, unicode):
                user = user.encode("utf-8")
            secret += user[:4]

        # null-pad or truncate to 16 bytes
        secret = right_pad_string(secret, 16)

        # md5 digest
        hash = md5(secret).digest()

        # drop every 4th byte
        hash = join_byte_elems(c for i, c in enumerate(hash) if i & 3 < 3)

        # encode using Hash64
        return h64.encode_bytes(hash).decode("ascii")
예제 #3
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)
         ]))
예제 #4
0
 def _calc_checksum(self, secret):
     if isinstance(secret, pl_unicode):
         secret = secret.encode("utf-8")
     real_rounds = 1 << self.rounds
     result = hashlib.sha512(self.salt.encode("ascii") + secret).digest()
     r = 0
     while r < real_rounds:
         result = hashlib.sha512(result + secret).digest()
         r += 1
     return h64.encode_bytes(result).decode("ascii")[:self.checksum_size]
예제 #5
0
 def _calc_checksum(self, secret):
     if isinstance(secret, pl_unicode):
         secret = secret.encode("utf-8")
     real_rounds = 1 << self.rounds
     result = hashlib.sha512(self.salt.encode("ascii") + secret).digest()
     r = 0
     while r < real_rounds:
         result = hashlib.sha512(result + secret).digest()
         r += 1
     return h64.encode_bytes(result).decode("ascii")[:self.checksum_size]
예제 #6
0
 def _calc_checksum(self, secret):
     #FIXME: can't find definitive policy on how phpass handles non-ascii.
     if isinstance(secret, unicode):
         secret = secret.encode("utf-8")
     real_rounds = 1<<self.rounds
     result = md5(self.salt.encode("ascii") + secret).digest()
     r = 0
     while r < real_rounds:
         result = md5(result + secret).digest()
         r += 1
     return h64.encode_bytes(result).decode("ascii")
예제 #7
0
파일: hashers.py 프로젝트: RestAuth/server
 def _calc_checksum(self, secret):
     # FIXME: can't find definitive policy on how phpass handles non-ascii.
     if isinstance(secret, six.text_type):  # pragma: no branch
         secret = secret.encode("utf-8")
     real_rounds = 1 << self.rounds
     result = hashlib.sha512(self.salt.encode('ascii') + secret).digest()
     r = 0
     while r < real_rounds:
         result = hashlib.sha512(result + secret).digest()
         r += 1
     return h64.encode_bytes(result)[:55 - 12].decode('ascii')
예제 #8
0
 def calc_checksum(self, secret):
     #FIXME: can't find definitive policy on how phpass handles non-ascii.
     if isinstance(secret, unicode):
         secret = secret.encode("utf-8")
     real_rounds = 1 << self.rounds
     result = md5(self.salt.encode("ascii") + secret).digest()
     r = 0
     while r < real_rounds:
         result = md5(result + secret).digest()
         r += 1
     return h64.encode_bytes(result).decode("ascii")
예제 #9
0
 def _calc_checksum(self, secret):
     # FIXME: can't find definitive policy on how phpass handles non-ascii.
     if isinstance(secret, six.text_type):  # pragma: no branch
         secret = secret.encode("utf-8")
     real_rounds = 1 << self.rounds
     result = hashlib.sha512(self.salt.encode('ascii') +
                             secret).digest()
     r = 0
     while r < real_rounds:
         result = hashlib.sha512(result + secret).digest()
         r += 1
     return h64.encode_bytes(result)[:55 - 12].decode('ascii')
예제 #10
0
 def test_decode_transposed_bytes_bad(self):
     for input, _, offsets in self.encode_transposed_dups:
         tmp = h64.encode_bytes(input)
         self.assertRaises(TypeError, h64.decode_transposed_bytes, tmp,
                           offsets)
예제 #11
0
 def test_decode_transposed_bytes(self):
     for input, result, offsets in self.encode_transposed:
         tmp = h64.encode_bytes(input)
         out = h64.decode_transposed_bytes(tmp, offsets)
         self.assertEqual(out, result)
예제 #12
0
 def test_encode_bytes(self):
     for source, result in self.encoded_bytes:
         out = h64.encode_bytes(source)
         self.assertEqual(out, result)
예제 #13
0
 def test_decode_transposed_bytes_bad(self):
     for input, _, offsets in self.encode_transposed_dups:
         tmp = h64.encode_bytes(input)
         self.assertRaises(TypeError, h64.decode_transposed_bytes, tmp, offsets)
예제 #14
0
 def test_decode_transposed_bytes(self):
     for input, result, offsets in self.encode_transposed:
         tmp = h64.encode_bytes(input)
         out = h64.decode_transposed_bytes(tmp, offsets)
         self.assertEqual(out, result)
예제 #15
0
 def test_encode_bytes(self):
     for source, result in self.encoded_bytes:
         out = h64.encode_bytes(source)
         self.assertEqual(out, result)