37
37
//! ```
38
38
use cfg_if:: cfg_if;
39
39
use libc:: c_void;
40
- use std:: mem;
40
+ use std:: mem:: MaybeUninit ;
41
41
42
42
/// Computes the SHA1 hash of some data.
43
43
///
@@ -46,23 +46,21 @@ use std::mem;
46
46
/// SHA1 is known to be insecure - it should not be used unless required for
47
47
/// compatibility with existing systems.
48
48
#[ inline]
49
- #[ allow( deprecated) ] // https://github.com/rust-lang/rust/issues/63566
50
49
pub fn sha1 ( data : & [ u8 ] ) -> [ u8 ; 20 ] {
51
50
unsafe {
52
- let mut hash: [ u8 ; 20 ] = mem :: uninitialized ( ) ;
53
- ffi:: SHA1 ( data. as_ptr ( ) , data. len ( ) , hash. as_mut_ptr ( ) ) ;
54
- hash
51
+ let mut hash = MaybeUninit :: < [ u8 ; 20 ] > :: uninit ( ) ;
52
+ ffi:: SHA1 ( data. as_ptr ( ) , data. len ( ) , hash. as_mut_ptr ( ) . cast ( ) ) ;
53
+ hash. assume_init ( )
55
54
}
56
55
}
57
56
58
57
/// Computes the SHA224 hash of some data.
59
58
#[ inline]
60
- #[ allow( deprecated) ] // https://github.com/rust-lang/rust/issues/63566
61
59
pub fn sha224 ( data : & [ u8 ] ) -> [ u8 ; 28 ] {
62
60
unsafe {
63
- let mut hash: [ u8 ; 28 ] = mem :: uninitialized ( ) ;
64
- ffi:: SHA224 ( data. as_ptr ( ) , data. len ( ) , hash. as_mut_ptr ( ) ) ;
65
- hash
61
+ let mut hash = MaybeUninit :: < [ u8 ; 28 ] > :: uninit ( ) ;
62
+ ffi:: SHA224 ( data. as_ptr ( ) , data. len ( ) , hash. as_mut_ptr ( ) . cast ( ) ) ;
63
+ hash. assume_init ( )
66
64
}
67
65
}
68
66
@@ -71,9 +69,9 @@ pub fn sha224(data: &[u8]) -> [u8; 28] {
71
69
#[ allow( deprecated) ] // https://github.com/rust-lang/rust/issues/63566
72
70
pub fn sha256 ( data : & [ u8 ] ) -> [ u8 ; 32 ] {
73
71
unsafe {
74
- let mut hash: [ u8 ; 32 ] = mem :: uninitialized ( ) ;
75
- ffi:: SHA256 ( data. as_ptr ( ) , data. len ( ) , hash. as_mut_ptr ( ) ) ;
76
- hash
72
+ let mut hash = MaybeUninit :: < [ u8 ; 32 ] > :: uninit ( ) ;
73
+ ffi:: SHA256 ( data. as_ptr ( ) , data. len ( ) , hash. as_mut_ptr ( ) . cast ( ) ) ;
74
+ hash. assume_init ( )
77
75
}
78
76
}
79
77
@@ -82,9 +80,9 @@ pub fn sha256(data: &[u8]) -> [u8; 32] {
82
80
#[ allow( deprecated) ] // https://github.com/rust-lang/rust/issues/63566
83
81
pub fn sha384 ( data : & [ u8 ] ) -> [ u8 ; 48 ] {
84
82
unsafe {
85
- let mut hash: [ u8 ; 48 ] = mem :: uninitialized ( ) ;
86
- ffi:: SHA384 ( data. as_ptr ( ) , data. len ( ) , hash. as_mut_ptr ( ) ) ;
87
- hash
83
+ let mut hash = MaybeUninit :: < [ u8 ; 48 ] > :: uninit ( ) ;
84
+ ffi:: SHA384 ( data. as_ptr ( ) , data. len ( ) , hash. as_mut_ptr ( ) . cast ( ) ) ;
85
+ hash. assume_init ( )
88
86
}
89
87
}
90
88
@@ -93,9 +91,9 @@ pub fn sha384(data: &[u8]) -> [u8; 48] {
93
91
#[ allow( deprecated) ] // https://github.com/rust-lang/rust/issues/63566
94
92
pub fn sha512 ( data : & [ u8 ] ) -> [ u8 ; 64 ] {
95
93
unsafe {
96
- let mut hash: [ u8 ; 64 ] = mem :: uninitialized ( ) ;
97
- ffi:: SHA512 ( data. as_ptr ( ) , data. len ( ) , hash. as_mut_ptr ( ) ) ;
98
- hash
94
+ let mut hash = MaybeUninit :: < [ u8 ; 64 ] > :: uninit ( ) ;
95
+ ffi:: SHA512 ( data. as_ptr ( ) , data. len ( ) , hash. as_mut_ptr ( ) . cast ( ) ) ;
96
+ hash. assume_init ( )
99
97
}
100
98
}
101
99
@@ -120,12 +118,11 @@ cfg_if! {
120
118
impl Sha1 {
121
119
/// Creates a new hasher.
122
120
#[ inline]
123
- #[ allow( deprecated) ] // https://github.com/rust-lang/rust/issues/63566
124
121
pub fn new( ) -> Sha1 {
125
122
unsafe {
126
- let mut ctx = mem :: uninitialized ( ) ;
127
- ffi:: SHA1_Init ( & mut ctx) ;
128
- Sha1 ( ctx)
123
+ let mut ctx = MaybeUninit :: uninit ( ) ;
124
+ ffi:: SHA1_Init ( ctx. as_mut_ptr ( ) ) ;
125
+ Sha1 ( ctx. assume_init ( ) )
129
126
}
130
127
}
131
128
@@ -141,12 +138,11 @@ cfg_if! {
141
138
142
139
/// Returns the hash of the data.
143
140
#[ inline]
144
- #[ allow( deprecated) ] // https://github.com/rust-lang/rust/issues/63566
145
141
pub fn finish( mut self ) -> [ u8 ; 20 ] {
146
142
unsafe {
147
- let mut hash: [ u8 ; 20 ] = mem :: uninitialized ( ) ;
148
- ffi:: SHA1_Final ( hash. as_mut_ptr( ) , & mut self . 0 ) ;
149
- hash
143
+ let mut hash = MaybeUninit :: < [ u8 ; 20 ] > :: uninit ( ) ;
144
+ ffi:: SHA1_Final ( hash. as_mut_ptr( ) . cast ( ) , & mut self . 0 ) ;
145
+ hash. assume_init ( )
150
146
}
151
147
}
152
148
}
@@ -165,12 +161,11 @@ cfg_if! {
165
161
impl Sha224 {
166
162
/// Creates a new hasher.
167
163
#[ inline]
168
- #[ allow( deprecated) ] // https://github.com/rust-lang/rust/issues/63566
169
164
pub fn new( ) -> Sha224 {
170
165
unsafe {
171
- let mut ctx = mem :: uninitialized ( ) ;
172
- ffi:: SHA224_Init ( & mut ctx) ;
173
- Sha224 ( ctx)
166
+ let mut ctx = MaybeUninit :: uninit ( ) ;
167
+ ffi:: SHA224_Init ( ctx. as_mut_ptr ( ) ) ;
168
+ Sha224 ( ctx. assume_init ( ) )
174
169
}
175
170
}
176
171
@@ -186,12 +181,11 @@ cfg_if! {
186
181
187
182
/// Returns the hash of the data.
188
183
#[ inline]
189
- #[ allow( deprecated) ] // https://github.com/rust-lang/rust/issues/63566
190
184
pub fn finish( mut self ) -> [ u8 ; 28 ] {
191
185
unsafe {
192
- let mut hash: [ u8 ; 28 ] = mem :: uninitialized ( ) ;
193
- ffi:: SHA224_Final ( hash. as_mut_ptr( ) , & mut self . 0 ) ;
194
- hash
186
+ let mut hash = MaybeUninit :: < [ u8 ; 28 ] > :: uninit ( ) ;
187
+ ffi:: SHA224_Final ( hash. as_mut_ptr( ) . cast ( ) , & mut self . 0 ) ;
188
+ hash. assume_init ( )
195
189
}
196
190
}
197
191
}
@@ -210,12 +204,11 @@ cfg_if! {
210
204
impl Sha256 {
211
205
/// Creates a new hasher.
212
206
#[ inline]
213
- #[ allow( deprecated) ] // https://github.com/rust-lang/rust/issues/63566
214
207
pub fn new( ) -> Sha256 {
215
208
unsafe {
216
- let mut ctx = mem :: uninitialized ( ) ;
217
- ffi:: SHA256_Init ( & mut ctx) ;
218
- Sha256 ( ctx)
209
+ let mut ctx = MaybeUninit :: uninit ( ) ;
210
+ ffi:: SHA256_Init ( ctx. as_mut_ptr ( ) ) ;
211
+ Sha256 ( ctx. assume_init ( ) )
219
212
}
220
213
}
221
214
@@ -231,12 +224,11 @@ cfg_if! {
231
224
232
225
/// Returns the hash of the data.
233
226
#[ inline]
234
- #[ allow( deprecated) ] // https://github.com/rust-lang/rust/issues/63566
235
227
pub fn finish( mut self ) -> [ u8 ; 32 ] {
236
228
unsafe {
237
- let mut hash: [ u8 ; 32 ] = mem :: uninitialized ( ) ;
238
- ffi:: SHA256_Final ( hash. as_mut_ptr( ) , & mut self . 0 ) ;
239
- hash
229
+ let mut hash = MaybeUninit :: < [ u8 ; 32 ] > :: uninit ( ) ;
230
+ ffi:: SHA256_Final ( hash. as_mut_ptr( ) . cast ( ) , & mut self . 0 ) ;
231
+ hash. assume_init ( )
240
232
}
241
233
}
242
234
}
@@ -255,12 +247,11 @@ cfg_if! {
255
247
impl Sha384 {
256
248
/// Creates a new hasher.
257
249
#[ inline]
258
- #[ allow( deprecated) ] // https://github.com/rust-lang/rust/issues/63566
259
250
pub fn new( ) -> Sha384 {
260
251
unsafe {
261
- let mut ctx = mem :: uninitialized ( ) ;
262
- ffi:: SHA384_Init ( & mut ctx) ;
263
- Sha384 ( ctx)
252
+ let mut ctx = MaybeUninit :: uninit ( ) ;
253
+ ffi:: SHA384_Init ( ctx. as_mut_ptr ( ) ) ;
254
+ Sha384 ( ctx. assume_init ( ) )
264
255
}
265
256
}
266
257
@@ -276,12 +267,11 @@ cfg_if! {
276
267
277
268
/// Returns the hash of the data.
278
269
#[ inline]
279
- #[ allow( deprecated) ] // https://github.com/rust-lang/rust/issues/63566
280
270
pub fn finish( mut self ) -> [ u8 ; 48 ] {
281
271
unsafe {
282
- let mut hash: [ u8 ; 48 ] = mem :: uninitialized ( ) ;
283
- ffi:: SHA384_Final ( hash. as_mut_ptr( ) , & mut self . 0 ) ;
284
- hash
272
+ let mut hash = MaybeUninit :: < [ u8 ; 48 ] > :: uninit ( ) ;
273
+ ffi:: SHA384_Final ( hash. as_mut_ptr( ) . cast ( ) , & mut self . 0 ) ;
274
+ hash. assume_init ( )
285
275
}
286
276
}
287
277
}
@@ -300,12 +290,11 @@ cfg_if! {
300
290
impl Sha512 {
301
291
/// Creates a new hasher.
302
292
#[ inline]
303
- #[ allow( deprecated) ] // https://github.com/rust-lang/rust/issues/63566
304
293
pub fn new( ) -> Sha512 {
305
294
unsafe {
306
- let mut ctx = mem :: uninitialized ( ) ;
307
- ffi:: SHA512_Init ( & mut ctx) ;
308
- Sha512 ( ctx)
295
+ let mut ctx = MaybeUninit :: uninit ( ) ;
296
+ ffi:: SHA512_Init ( ctx. as_mut_ptr ( ) ) ;
297
+ Sha512 ( ctx. assume_init ( ) )
309
298
}
310
299
}
311
300
@@ -321,12 +310,11 @@ cfg_if! {
321
310
322
311
/// Returns the hash of the data.
323
312
#[ inline]
324
- #[ allow( deprecated) ] // https://github.com/rust-lang/rust/issues/63566
325
313
pub fn finish( mut self ) -> [ u8 ; 64 ] {
326
314
unsafe {
327
- let mut hash: [ u8 ; 64 ] = mem :: uninitialized ( ) ;
328
- ffi:: SHA512_Final ( hash. as_mut_ptr( ) , & mut self . 0 ) ;
329
- hash
315
+ let mut hash= MaybeUninit :: < [ u8 ; 64 ] > :: uninit ( ) ;
316
+ ffi:: SHA512_Final ( hash. as_mut_ptr( ) . cast ( ) , & mut self . 0 ) ;
317
+ hash. assume_init ( )
330
318
}
331
319
}
332
320
}
0 commit comments