/usr/lib/python3/dist-packages/openid/cryptutil.py is in python3-openid 3.0.9-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | """Module containing a cryptographic-quality source of randomness and
other cryptographically useful functionality
Python 2.4 needs no external support for this module, nor does Python
2.3 on a system with /dev/urandom.
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
"""
__all__ = [
'base64ToLong',
'binaryToLong',
'hmacSha1',
'hmacSha256',
'longToBase64',
'longToBinary',
'randomString',
'randrange',
'sha1',
'sha256',
]
import hmac
import os
import random
from openid.oidutil import toBase64, fromBase64
import hashlib
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):
if isinstance(key, str):
key = bytes(key, encoding="utf-8")
if isinstance(text, str):
text = bytes(text, encoding="utf-8")
return hmac.new(key, text, sha1_module).digest()
def sha1(s):
if isinstance(s, str):
s = bytes(s, encoding="utf-8")
return sha1_module.new(s).digest()
def hmacSha256(key, text):
if isinstance(key, str):
key = bytes(key, encoding="utf-8")
if isinstance(text, str):
text = bytes(text, encoding="utf-8")
return hmac.new(key, text, sha256_module).digest()
def sha256(s):
if isinstance(s, str):
s = bytes(s, encoding="utf-8")
return sha256_module.new(s).digest()
SHA256_AVAILABLE = True
try:
from Crypto.Util.number import long_to_bytes, bytes_to_long
except ImportError:
# In the case where we don't have pycrypto installed, define substitute
# functionality.
import pickle
def longToBinary(l):
if l == 0:
return b'\x00'
b = bytearray(pickle.encode_long(l))
b.reverse()
return bytes(b)
def binaryToLong(s):
if isinstance(s, str):
s = s.encode("utf-8")
b = bytearray(s)
b.reverse()
return pickle.decode_long(bytes(b))
else:
# We have pycrypto, so wrap its functions instead.
def longToBinary(l):
if l < 0:
raise ValueError('This function only supports positive integers')
bytestring = long_to_bytes(l)
if bytestring[0] > 127:
return b'\x00' + bytestring
else:
return bytestring
def binaryToLong(bytestring):
if not bytestring:
raise ValueError('Empty string passed to strToLong')
if bytestring[0] > 127:
raise ValueError('This function only supports positive integers')
return bytes_to_long(bytestring)
# A cryptographically safe source of random bytes
getBytes = os.urandom
# A randrange function that works for longs
randrange = random.randrange
def longToBase64(l):
return toBase64(longToBinary(l))
def base64ToLong(s):
return binaryToLong(fromBase64(s))
def randomString(length, chrs=None):
"""Produce a string of length random bytes, chosen from chrs."""
if chrs is None:
return getBytes(length)
else:
n = len(chrs)
return ''.join([chrs[randrange(n)] for _ in range(length)])
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
|