fstdtools 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,130 @@
1
+ """
2
+ Copyright by https://github.com/zhansliu/writemdict
3
+
4
+ ripemd128.py - A simple ripemd128 library in pure Python.
5
+
6
+ Supports both Python 2 (versions >= 2.6) and Python 3.
7
+
8
+ Usage:
9
+ from ripemd128 import ripemd128
10
+ digest = ripemd128(b"The quick brown fox jumps over the lazy dog")
11
+ assert(digest == b"\x3f\xa9\xb5\x7f\x05\x3c\x05\x3f\xbe\x27\x35\xb2\x38\x0d\xb5\x96")
12
+
13
+ """
14
+
15
+
16
+
17
+ import struct
18
+
19
+
20
+ # follows this description: http://homes.esat.kuleuven.be/~bosselae/ripemd/rmd128.txt
21
+
22
+ def f(j, x, y, z):
23
+ assert(0 <= j and j < 64)
24
+ if j < 16:
25
+ return x ^ y ^ z
26
+ elif j < 32:
27
+ return (x & y) | (z & ~x)
28
+ elif j < 48:
29
+ return (x | (0xffffffff & ~y)) ^ z
30
+ else:
31
+ return (x & z) | (y & ~z)
32
+
33
+ def K(j):
34
+ assert(0 <= j and j < 64)
35
+ if j < 16:
36
+ return 0x00000000
37
+ elif j < 32:
38
+ return 0x5a827999
39
+ elif j < 48:
40
+ return 0x6ed9eba1
41
+ else:
42
+ return 0x8f1bbcdc
43
+
44
+ def Kp(j):
45
+ assert(0 <= j and j < 64)
46
+ if j < 16:
47
+ return 0x50a28be6
48
+ elif j < 32:
49
+ return 0x5c4dd124
50
+ elif j < 48:
51
+ return 0x6d703ef3
52
+ else:
53
+ return 0x00000000
54
+
55
+ def padandsplit(message):
56
+ """
57
+ returns a two-dimensional array X[i][j] of 32-bit integers, where j ranges
58
+ from 0 to 16.
59
+ First pads the message to length in bytes is congruent to 56 (mod 64),
60
+ by first adding a byte 0x80, and then padding with 0x00 bytes until the
61
+ message length is congruent to 56 (mod 64). Then adds the little-endian
62
+ 64-bit representation of the original length. Finally, splits the result
63
+ up into 64-byte blocks, which are further parsed as 32-bit integers.
64
+ """
65
+ origlen = len(message)
66
+ padlength = 64 - ((origlen - 56) % 64) #minimum padding is 1!
67
+ message += b"\x80"
68
+ message += b"\x00" * (padlength - 1)
69
+ message += struct.pack("<Q", origlen*8)
70
+ assert(len(message) % 64 == 0)
71
+ return [
72
+ [
73
+ struct.unpack("<L", message[i+j:i+j+4])[0]
74
+ for j in range(0, 64, 4)
75
+ ]
76
+ for i in range(0, len(message), 64)
77
+ ]
78
+
79
+
80
+ def add(*args):
81
+ return sum(args) & 0xffffffff
82
+
83
+ def rol(s,x):
84
+ assert(s < 32)
85
+ return (x << s | x >> (32-s)) & 0xffffffff
86
+
87
+ r = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,
88
+ 7, 4,13, 1,10, 6,15, 3,12, 0, 9, 5, 2,14,11, 8,
89
+ 3,10,14, 4, 9,15, 8, 1, 2, 7, 0, 6,13,11, 5,12,
90
+ 1, 9,11,10, 0, 8,12, 4,13, 3, 7,15,14, 5, 6, 2]
91
+ rp = [ 5,14, 7, 0, 9, 2,11, 4,13, 6,15, 8, 1,10, 3,12,
92
+ 6,11, 3, 7, 0,13, 5,10,14,15, 8,12, 4, 9, 1, 2,
93
+ 15, 5, 1, 3, 7,14, 6, 9,11, 8,12, 2,10, 0, 4,13,
94
+ 8, 6, 4, 1, 3,11,15, 0, 5,12, 2,13, 9, 7,10,14]
95
+ s = [11,14,15,12, 5, 8, 7, 9,11,13,14,15, 6, 7, 9, 8,
96
+ 7, 6, 8,13,11, 9, 7,15, 7,12,15, 9,11, 7,13,12,
97
+ 11,13, 6, 7,14, 9,13,15,14, 8,13, 6, 5,12, 7, 5,
98
+ 11,12,14,15,14,15, 9, 8, 9,14, 5, 6, 8, 6, 5,12]
99
+ sp = [ 8, 9, 9,11,13,15,15, 5, 7, 7, 8,11,14,14,12, 6,
100
+ 9,13,15, 7,12, 8, 9,11, 7, 7,12, 7, 6,15,13,11,
101
+ 9, 7,15,11, 8, 6, 6,14,12,13, 5,14,13,13, 7, 5,
102
+ 15, 5, 8,11,14,14, 6,14, 6, 9,12, 9,12, 5,15, 8]
103
+
104
+
105
+ def ripemd128(message):
106
+ h0 = 0x67452301
107
+ h1 = 0xefcdab89
108
+ h2 = 0x98badcfe
109
+ h3 = 0x10325476
110
+ X = padandsplit(message)
111
+ for i in range(len(X)):
112
+ (A,B,C,D) = (h0,h1,h2,h3)
113
+ (Ap,Bp,Cp,Dp) = (h0,h1,h2,h3)
114
+ for j in range(64):
115
+ T = rol(s[j], add(A, f(j,B,C,D), X[i][r[j]], K(j)))
116
+ (A,D,C,B) = (D,C,B,T)
117
+ T = rol(sp[j], add(Ap, f(63-j,Bp,Cp,Dp), X[i][rp[j]], Kp(j)))
118
+ (Ap,Dp,Cp,Bp)=(Dp,Cp,Bp,T)
119
+ T = add(h1,C,Dp)
120
+ h1 = add(h2,D,Ap)
121
+ h2 = add(h3,A,Bp)
122
+ h3 = add(h0,B,Cp)
123
+ h0 = T
124
+
125
+
126
+ return struct.pack("<LLLL",h0,h1,h2,h3)
127
+
128
+ def hexstr(bstr):
129
+ return "".join("{0:02x}".format(b) for b in bstr)
130
+