示例#1
0
def raw_sun_md5_crypt(secret, rounds, salt):
    global MAGIC_HAMLET
    if rounds <= 0:
        rounds = 0
    real_rounds = 4096 + rounds
    result = md5(secret + salt).digest()
    X_ROUNDS_0, X_ROUNDS_1, Y_ROUNDS_0, Y_ROUNDS_1 = _XY_ROUNDS
    round = 0
    while round < real_rounds:
        rval = [ byte_elem_value(c) for c in result ].__getitem__
        x = 0
        xrounds = X_ROUNDS_1 if rval(round >> 3 & 15) >> (round & 7) & 1 else X_ROUNDS_0
        for i, ia, ib in xrounds:
            a = rval(ia)
            b = rval(ib)
            v = rval(a >> b % 5 & 15) >> (b >> (a & 7) & 1)
            x |= (rval(v >> 3 & 15) >> (v & 7) & 1) << i

        y = 0
        yrounds = Y_ROUNDS_1 if rval(round + 64 >> 3 & 15) >> (round & 7) & 1 else Y_ROUNDS_0
        for i, ia, ib in yrounds:
            a = rval(ia)
            b = rval(ib)
            v = rval(a >> b % 5 & 15) >> (b >> (a & 7) & 1)
            y |= (rval(v >> 3 & 15) >> (v & 7) & 1) << i

        coin = (rval(x >> 3) >> (x & 7) ^ rval(y >> 3) >> (y & 7)) & 1
        h = md5(result)
        if coin:
            h.update(MAGIC_HAMLET)
        h.update(unicode(round).encode('ascii'))
        result = h.digest()
        round += 1

    return h64.encode_transposed_bytes(result, _chk_offsets)
示例#2
0
def _raw_sha2_crypt(pwd, salt, rounds, use_512=False):
    if isinstance(pwd, unicode):
        pwd = pwd.encode('utf-8')
    if _BNULL in pwd:
        raise uh.exc.NullPasswordError(
            sha512_crypt if use_512 else sha256_crypt)
    pwd_len = len(pwd)
    salt = salt.encode('ascii')
    salt_len = len(salt)
    if use_512:
        hash_const = hashlib.sha512
        transpose_map = _512_transpose_map
    else:
        hash_const = hashlib.sha256
        transpose_map = _256_transpose_map
    db = hash_const(pwd + salt + pwd).digest()
    a_ctx = hash_const(pwd + salt)
    a_ctx_update = a_ctx.update
    a_ctx_update(repeat_string(db, pwd_len))
    i = pwd_len
    while i:
        a_ctx_update(db if i & 1 else pwd)
        i >>= 1

    da = a_ctx.digest()
    if pwd_len < 96:
        dp = repeat_string(hash_const(pwd * pwd_len).digest(), pwd_len)
    else:
        tmp_ctx = hash_const(pwd)
        tmp_ctx_update = tmp_ctx.update
        i = pwd_len - 1
        while i:
            tmp_ctx_update(pwd)
            i -= 1

        dp = repeat_string(tmp_ctx.digest(), pwd_len)
    ds = hash_const(salt * (16 + byte_elem_value(da[0]))).digest()[:salt_len]
    dp_dp = dp + dp
    dp_ds = dp + ds
    perms = [dp, dp_dp, dp_ds, dp_ds + dp, ds + dp, ds + dp_dp]
    data = [(perms[even], perms[odd]) for even, odd in _c_digest_offsets]
    dc = da
    blocks, tail = divmod(rounds, 42)
    while blocks:
        for even, odd in data:
            dc = hash_const(odd + hash_const(dc + even).digest()).digest()

        blocks -= 1

    if tail:
        pairs = tail >> 1
        for even, odd in data[:pairs]:
            dc = hash_const(odd + hash_const(dc + even).digest()).digest()

        if tail & 1:
            dc = hash_const(dc + data[pairs][0]).digest()
    return h64.encode_transposed_bytes(dc, transpose_map).decode('ascii')
示例#3
0
 def _generate(self, counter):
     keyed_hmac = self._keyed_hmac
     if keyed_hmac is None:
         keyed_hmac = self._keyed_hmac = compile_hmac(self.alg, self.key)
     digest = keyed_hmac(_pack_uint64(counter))
     digest_size = keyed_hmac.digest_info.digest_size
     offset = byte_elem_value(digest[(-1)]) & 15
     value = _unpack_uint32(digest[offset:offset + 4])[0] & 2147483647
     digits = self.digits
     return (u('%0*d') % (digits, value))[-digits:]
示例#4
0
    def _calc_checksum(self, secret):
        if isinstance(secret, unicode):
            secret = secret.encode('utf-8')
        MASK_32 = 4294967295L
        MASK_31 = 2147483647
        WHITE = ' \t'
        nr1 = 1345345333
        nr2 = 305419889
        add = 7
        for c in secret:
            if c in WHITE:
                continue
            tmp = byte_elem_value(c)
            nr1 ^= ((nr1 & 63) + add) * tmp + (nr1 << 8) & MASK_32
            nr2 = nr2 + (nr2 << 8 ^ nr1) & MASK_32
            add = add + tmp & MASK_32

        return u('%08x%08x') % (nr1 & MASK_31, nr2 & MASK_31)
示例#5
0
def _crypt_secret_to_key(secret):
    return sum((byte_elem_value(c) & 127) << 57 - i * 8
               for i, c in enumerate(secret[:8]))