blob: 7e8cd0ac85f0dc553a2f60fac0d246d80b171060 (
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
|
"""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.
"""
from __future__ import unicode_literals
import codecs
import warnings
from openid.oidutil import fromBase64, toBase64
__all__ = [
'base64ToLong',
'binaryToLong',
'longToBase64',
'longToBinary',
'int_to_bytes',
'bytes_to_int',
]
def bytes_to_int(value):
"""
Convert byte string to integer.
@type value: six.binary_type
@rtype: Union[six.integer_types]
"""
return int(codecs.encode(value, 'hex'), 16)
def fix_btwoc(value):
"""
Utility function to ensure the output conforms the `btwoc` function output.
See http://openid.net/specs/openid-authentication-2_0.html#btwoc for details.
@type value: bytes or bytearray
@rtype: bytes
"""
# Conversion to bytearray is python 2/3 compatible
array = bytearray(value)
# First bit must be zero. If it isn't, the bytes must be prepended by zero byte.
if array[0] > 127:
array = bytearray([0]) + array
return bytes(array)
def int_to_bytes(value):
"""
Convert integer to byte string.
@type value: Union[six.integer_types]
@rtype: six.binary_type
"""
hex_value = '{:x}'.format(value)
if len(hex_value) % 2:
hex_value = '0' + hex_value
array = bytearray.fromhex(hex_value)
# The output must be `btwoc` compatible
return fix_btwoc(array)
# Deprecated versions of bytes <--> int conversions
def longToBinary(value):
warnings.warn("Function longToBinary is deprecated in favor of int_to_bytes.", DeprecationWarning)
return int_to_bytes(value)
def binaryToLong(s):
warnings.warn("Function binaryToLong is deprecated in favor of bytes_to_int.", DeprecationWarning)
return bytes_to_int(s)
def longToBase64(value):
return toBase64(int_to_bytes(value))
def base64ToLong(s):
return bytes_to_int(fromBase64(s))
|