@@ -13,7 +13,36 @@ mod hashlib {
13
13
use md5:: Md5 ;
14
14
use sha1:: Sha1 ;
15
15
use sha2:: { Sha224 , Sha256 , Sha384 , Sha512 } ;
16
- use sha3:: { Sha3_224 , Sha3_256 , Sha3_384 , Sha3_512 } ; // TODO: , Shake128, Shake256;
16
+ use sha3:: { Sha3_224 , Sha3_256 , Sha3_384 , Sha3_512 } ; // TODO: , shake_128, shake_256;
17
+
18
+ #[ derive( FromArgs ) ]
19
+ #[ allow( unused) ]
20
+ struct NewHashArgs {
21
+ #[ pyarg( positional) ]
22
+ name : PyStrRef ,
23
+ #[ pyarg( any, optional) ]
24
+ data : OptionalArg < PyBytesRef > ,
25
+ #[ pyarg( named, default = "true" ) ]
26
+ usedforsecurity : bool ,
27
+ }
28
+
29
+ #[ derive( FromArgs ) ]
30
+ #[ allow( unused) ]
31
+ struct BlakeHashArgs {
32
+ #[ pyarg( positional, optional) ]
33
+ data : OptionalArg < PyBytesRef > ,
34
+ #[ pyarg( named, default = "true" ) ]
35
+ usedforsecurity : bool ,
36
+ }
37
+
38
+ #[ derive( FromArgs ) ]
39
+ #[ allow( unused) ]
40
+ struct HashArgs {
41
+ #[ pyarg( any, optional) ]
42
+ string : OptionalArg < PyBytesRef > ,
43
+ #[ pyarg( named, default = "true" ) ]
44
+ usedforsecurity : bool ,
45
+ }
17
46
18
47
#[ pyattr]
19
48
#[ pyclass( module = "hashlib" , name = "hasher" ) ]
@@ -83,26 +112,58 @@ mod hashlib {
83
112
}
84
113
85
114
#[ pyfunction( name = "new" ) ]
86
- fn hashlib_new (
87
- name : PyStrRef ,
88
- data : OptionalArg < PyBytesRef > ,
89
- vm : & VirtualMachine ,
90
- ) -> PyResult < PyHasher > {
91
- match name. as_str ( ) {
92
- "md5" => md5 ( data) ,
93
- "sha1" => sha1 ( data) ,
94
- "sha224" => sha224 ( data) ,
95
- "sha256" => sha256 ( data) ,
96
- "sha384" => sha384 ( data) ,
97
- "sha512" => sha512 ( data) ,
98
- "sha3_224" => sha3_224 ( data) ,
99
- "sha3_256" => sha3_256 ( data) ,
100
- "sha3_384" => sha3_384 ( data) ,
101
- "sha3_512" => sha3_512 ( data) ,
102
- // TODO: "shake128" => shake128(data, ),
103
- // TODO: "shake256" => shake256(data, ),
104
- "blake2b" => blake2b ( data) ,
105
- "blake2s" => blake2s ( data) ,
115
+ fn hashlib_new ( args : NewHashArgs , vm : & VirtualMachine ) -> PyResult < PyHasher > {
116
+ match args. name . as_str ( ) {
117
+ "md5" => md5 ( HashArgs {
118
+ string : args. data ,
119
+ usedforsecurity : args. usedforsecurity ,
120
+ } ) ,
121
+ "sha1" => sha1 ( HashArgs {
122
+ string : args. data ,
123
+ usedforsecurity : args. usedforsecurity ,
124
+ } ) ,
125
+ "sha224" => sha224 ( HashArgs {
126
+ string : args. data ,
127
+ usedforsecurity : args. usedforsecurity ,
128
+ } ) ,
129
+ "sha256" => sha256 ( HashArgs {
130
+ string : args. data ,
131
+ usedforsecurity : args. usedforsecurity ,
132
+ } ) ,
133
+ "sha384" => sha384 ( HashArgs {
134
+ string : args. data ,
135
+ usedforsecurity : args. usedforsecurity ,
136
+ } ) ,
137
+ "sha512" => sha512 ( HashArgs {
138
+ string : args. data ,
139
+ usedforsecurity : args. usedforsecurity ,
140
+ } ) ,
141
+ "sha3_224" => sha3_224 ( HashArgs {
142
+ string : args. data ,
143
+ usedforsecurity : args. usedforsecurity ,
144
+ } ) ,
145
+ "sha3_256" => sha3_256 ( HashArgs {
146
+ string : args. data ,
147
+ usedforsecurity : args. usedforsecurity ,
148
+ } ) ,
149
+ "sha3_384" => sha3_384 ( HashArgs {
150
+ string : args. data ,
151
+ usedforsecurity : args. usedforsecurity ,
152
+ } ) ,
153
+ "sha3_512" => sha3_512 ( HashArgs {
154
+ string : args. data ,
155
+ usedforsecurity : args. usedforsecurity ,
156
+ } ) ,
157
+ // TODO: "shake_128" => shake_128(args.data, ),
158
+ // TODO: "shake_256" => shake_256(args.data, ),
159
+ "blake2b" => blake2b ( BlakeHashArgs {
160
+ data : args. data ,
161
+ usedforsecurity : args. usedforsecurity ,
162
+ } ) ,
163
+ "blake2s" => blake2s ( BlakeHashArgs {
164
+ data : args. data ,
165
+ usedforsecurity : args. usedforsecurity ,
166
+ } ) ,
106
167
other => Err ( vm. new_value_error ( format ! ( "Unknown hashing algorithm: {}" , other) ) ) ,
107
168
}
108
169
}
@@ -116,75 +177,87 @@ mod hashlib {
116
177
}
117
178
118
179
#[ pyfunction]
119
- fn md5 ( data : OptionalArg < PyBytesRef > ) -> PyResult < PyHasher > {
120
- init ( PyHasher :: new ( "md5" , HashWrapper :: md5 ( ) ) , data )
180
+ fn md5 ( args : HashArgs ) -> PyResult < PyHasher > {
181
+ init ( PyHasher :: new ( "md5" , HashWrapper :: md5 ( ) ) , args . string )
121
182
}
122
183
123
184
#[ pyfunction]
124
- fn sha1 ( data : OptionalArg < PyBytesRef > ) -> PyResult < PyHasher > {
125
- init ( PyHasher :: new ( "sha1" , HashWrapper :: sha1 ( ) ) , data )
185
+ fn sha1 ( args : HashArgs ) -> PyResult < PyHasher > {
186
+ init ( PyHasher :: new ( "sha1" , HashWrapper :: sha1 ( ) ) , args . string )
126
187
}
127
188
128
189
#[ pyfunction]
129
- fn sha224 ( data : OptionalArg < PyBytesRef > ) -> PyResult < PyHasher > {
130
- init ( PyHasher :: new ( "sha224" , HashWrapper :: sha224 ( ) ) , data )
190
+ fn sha224 ( args : HashArgs ) -> PyResult < PyHasher > {
191
+ init ( PyHasher :: new ( "sha224" , HashWrapper :: sha224 ( ) ) , args . string )
131
192
}
132
193
133
194
#[ pyfunction]
134
- fn sha256 ( data : OptionalArg < PyBytesRef > ) -> PyResult < PyHasher > {
135
- init ( PyHasher :: new ( "sha256" , HashWrapper :: sha256 ( ) ) , data )
195
+ fn sha256 ( args : HashArgs ) -> PyResult < PyHasher > {
196
+ init ( PyHasher :: new ( "sha256" , HashWrapper :: sha256 ( ) ) , args . string )
136
197
}
137
198
138
199
#[ pyfunction]
139
- fn sha384 ( data : OptionalArg < PyBytesRef > ) -> PyResult < PyHasher > {
140
- init ( PyHasher :: new ( "sha384" , HashWrapper :: sha384 ( ) ) , data )
200
+ fn sha384 ( args : HashArgs ) -> PyResult < PyHasher > {
201
+ init ( PyHasher :: new ( "sha384" , HashWrapper :: sha384 ( ) ) , args . string )
141
202
}
142
203
143
204
#[ pyfunction]
144
- fn sha512 ( data : OptionalArg < PyBytesRef > ) -> PyResult < PyHasher > {
145
- init ( PyHasher :: new ( "sha512" , HashWrapper :: sha512 ( ) ) , data )
205
+ fn sha512 ( args : HashArgs ) -> PyResult < PyHasher > {
206
+ init ( PyHasher :: new ( "sha512" , HashWrapper :: sha512 ( ) ) , args . string )
146
207
}
147
208
148
209
#[ pyfunction]
149
- fn sha3_224 ( data : OptionalArg < PyBytesRef > ) -> PyResult < PyHasher > {
150
- init ( PyHasher :: new ( "sha3_224" , HashWrapper :: sha3_224 ( ) ) , data)
210
+ fn sha3_224 ( args : HashArgs ) -> PyResult < PyHasher > {
211
+ init (
212
+ PyHasher :: new ( "sha3_224" , HashWrapper :: sha3_224 ( ) ) ,
213
+ args. string ,
214
+ )
151
215
}
152
216
153
217
#[ pyfunction]
154
- fn sha3_256 ( data : OptionalArg < PyBytesRef > ) -> PyResult < PyHasher > {
155
- init ( PyHasher :: new ( "sha3_256" , HashWrapper :: sha3_256 ( ) ) , data)
218
+ fn sha3_256 ( args : HashArgs ) -> PyResult < PyHasher > {
219
+ init (
220
+ PyHasher :: new ( "sha3_256" , HashWrapper :: sha3_256 ( ) ) ,
221
+ args. string ,
222
+ )
156
223
}
157
224
158
225
#[ pyfunction]
159
- fn sha3_384 ( data : OptionalArg < PyBytesRef > ) -> PyResult < PyHasher > {
160
- init ( PyHasher :: new ( "sha3_384" , HashWrapper :: sha3_384 ( ) ) , data)
226
+ fn sha3_384 ( args : HashArgs ) -> PyResult < PyHasher > {
227
+ init (
228
+ PyHasher :: new ( "sha3_384" , HashWrapper :: sha3_384 ( ) ) ,
229
+ args. string ,
230
+ )
161
231
}
162
232
163
233
#[ pyfunction]
164
- fn sha3_512 ( data : OptionalArg < PyBytesRef > ) -> PyResult < PyHasher > {
165
- init ( PyHasher :: new ( "sha3_512" , HashWrapper :: sha3_512 ( ) ) , data)
234
+ fn sha3_512 ( args : HashArgs ) -> PyResult < PyHasher > {
235
+ init (
236
+ PyHasher :: new ( "sha3_512" , HashWrapper :: sha3_512 ( ) ) ,
237
+ args. string ,
238
+ )
166
239
}
167
240
168
241
#[ pyfunction]
169
- fn shake128 ( _data : OptionalArg < PyBytesRef > , vm : & VirtualMachine ) -> PyResult < PyHasher > {
170
- Err ( vm. new_not_implemented_error ( "shake256 " . to_owned ( ) ) )
242
+ fn shake_128 ( _args : HashArgs , vm : & VirtualMachine ) -> PyResult < PyHasher > {
243
+ Err ( vm. new_not_implemented_error ( "shake_256 " . to_owned ( ) ) )
171
244
}
172
245
173
246
#[ pyfunction]
174
- fn shake256 ( _data : OptionalArg < PyBytesRef > , vm : & VirtualMachine ) -> PyResult < PyHasher > {
175
- Err ( vm. new_not_implemented_error ( "shake256 " . to_owned ( ) ) )
247
+ fn shake_256 ( _args : HashArgs , vm : & VirtualMachine ) -> PyResult < PyHasher > {
248
+ Err ( vm. new_not_implemented_error ( "shake_256 " . to_owned ( ) ) )
176
249
}
177
250
178
251
#[ pyfunction]
179
- fn blake2b ( data : OptionalArg < PyBytesRef > ) -> PyResult < PyHasher > {
252
+ fn blake2b ( args : BlakeHashArgs ) -> PyResult < PyHasher > {
180
253
// TODO: handle parameters
181
- init ( PyHasher :: new ( "blake2b" , HashWrapper :: blake2b ( ) ) , data)
254
+ init ( PyHasher :: new ( "blake2b" , HashWrapper :: blake2b ( ) ) , args . data )
182
255
}
183
256
184
257
#[ pyfunction]
185
- fn blake2s ( data : OptionalArg < PyBytesRef > ) -> PyResult < PyHasher > {
258
+ fn blake2s ( args : BlakeHashArgs ) -> PyResult < PyHasher > {
186
259
// TODO: handle parameters
187
- init ( PyHasher :: new ( "blake2s" , HashWrapper :: blake2s ( ) ) , data)
260
+ init ( PyHasher :: new ( "blake2s" , HashWrapper :: blake2s ( ) ) , args . data )
188
261
}
189
262
190
263
trait ThreadSafeDynDigest : DynDigest + Sync + Send { }
@@ -244,12 +317,12 @@ mod hashlib {
244
317
}
245
318
246
319
/* TODO:
247
- fn shake128 () -> Self {
248
- Self::new(Shake128 ::default())
320
+ fn shake_128 () -> Self {
321
+ Self::new(shake_128 ::default())
249
322
}
250
323
251
- fn shake256 () -> Self {
252
- Self::new(Shake256 ::default())
324
+ fn shake_256 () -> Self {
325
+ Self::new(shake_256 ::default())
253
326
}
254
327
*/
255
328
fn blake2b ( ) -> Self {
0 commit comments