summaryrefslogtreecommitdiff
path: root/openid/cryptutil.py
blob: 3fddee6a38d74eb24d0f9db7151aee6bb6c417e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
"""Module containing a cryptographic-quality source of randomness and
other cryptographically useful functionality

Other configurations will need a quality source of random bytes and
access to a function that will convert binary strings to long
integers. This module will work with the Python Cryptography Toolkit
(pycrypto) if it is present. pycrypto can be found with a search
engine, but is currently found at:

http://www.amk.ca/python/code/crypto
"""
from __future__ import unicode_literals

import hashlib
import hmac
import os
import random

from openid.oidutil import fromBase64, string_to_text, toBase64

__all__ = [
    'base64ToLong',
    'binaryToLong',
    'hmacSha1',
    'hmacSha256',
    'longToBase64',
    'longToBinary',
    'randrange',
    'sha1',
    'sha256',
]


class HashContainer(object):
    def __init__(self, hash_constructor):
        self.new = hash_constructor
        self.digest_size = hash_constructor().digest_size


sha1_module = HashContainer(hashlib.sha1)
sha256_module = HashContainer(hashlib.sha256)


def hmacSha1(key, text):
    """
    Return a SHA1 HMAC.

    @type key: six.binary_type
    @type text: six.text_type, six.binary_type is deprecated
    @rtype: six.binary_type
    """
    text = string_to_text(text, "Binary values for text are deprecated. Use text input instead.")
    return hmac.new(key, text.encode('utf-8'), sha1_module).digest()


def sha1(s):
    """
    Return a SHA1 hash.

    @type s: six.binary_type
    @rtype: six.binary_type
    """
    return sha1_module.new(s).digest()


def hmacSha256(key, text):
    """
    Return a SHA256 HMAC.

    @type key: six.binary_type
    @type text: six.text_type, six.binary_type is deprecated
    @rtype: six.binary_type
    """
    text = string_to_text(text, "Binary values for text are deprecated. Use text input instead.")
    return hmac.new(key, text.encode('utf-8'), sha256_module).digest()


def sha256(s):
    """
    Return a SHA256 hash.

    @type s: six.binary_type
    @rtype: six.binary_type
    """
    return sha256_module.new(s).digest()


try:
    from Crypto.Util.number import long_to_bytes, bytes_to_long
except ImportError:
    import pickle

    def longToBinary(value):
        if value == 0:
            return b'\x00'

        return pickle.encode_long(value)[::-1]

    def binaryToLong(s):
        return pickle.decode_long(s[::-1])
else:
    # We have pycrypto

    def longToBinary(value):
        if value < 0:
            raise ValueError('This function only supports positive integers')

        output = long_to_bytes(value)
        if isinstance(output[0], int):
            ord_first = output[0]
        else:
            ord_first = ord(output[0])
        if ord_first > 127:
            return b'\x00' + output
        else:
            return output

    def binaryToLong(s):
        if not s:
            raise ValueError('Empty string passed to strToLong')

        if isinstance(s[0], int):
            ord_first = s[0]
        else:
            ord_first = ord(s[0])
        if ord_first > 127:
            raise ValueError('This function only supports positive integers')

        return bytes_to_long(s)


# A randrange function that works for longs
try:
    randrange = random.SystemRandom().randrange
except AttributeError:
    # In Python 2.2's random.Random, randrange does not support
    # numbers larger than sys.maxint for randrange. For simplicity,
    # use this implementation for any Python that does not have
    # random.SystemRandom

    _duplicate_cache = {}

    def randrange(start, stop=None, step=1):
        if stop is None:
            stop = start
            start = 0

        r = (stop - start) // step
        try:
            (duplicate, nbytes) = _duplicate_cache[r]
        except KeyError:
            rbytes = longToBinary(r)
            if rbytes[0] == '\x00':
                nbytes = len(rbytes) - 1
            else:
                nbytes = len(rbytes)

            mxrand = (256 ** nbytes)

            # If we get a number less than this, then it is in the
            # duplicated range.
            duplicate = mxrand % r

            if len(_duplicate_cache) > 10:
                _duplicate_cache.clear()

            _duplicate_cache[r] = (duplicate, nbytes)

        while True:
            bytes = '\x00' + os.urandom(nbytes)
            n = binaryToLong(bytes)
            # Keep looping if this value is in the low duplicated range
            if n >= duplicate:
                break

        return start + (n % r) * step


def longToBase64(l):
    return toBase64(longToBinary(l))


def base64ToLong(s):
    return binaryToLong(fromBase64(s))


def const_eq(s1, s2):
    if len(s1) != len(s2):
        return False

    result = True
    for i in range(len(s1)):
        result = result and (s1[i] == s2[i])

    return result