Skip to content

Commit 2539bb7

Browse files
emgreXanewok
authored andcommitted
Check feature flags, bump MSRV to 1.37 and apply formating.
1 parent bb9511d commit 2539bb7

File tree

6 files changed

+278
-189
lines changed

6 files changed

+278
-189
lines changed

.github/workflows/rust.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@ jobs:
66
strategy:
77
matrix:
88
rust:
9-
- 1.34.0 # MSRV
9+
- 1.37.0 # MSRV
1010
- stable
1111
- beta
12+
features:
13+
- ""
14+
- "zeroize"
15+
exclude: # Zeroize 1.1 supports Rust 1.39+
16+
- rust: 1.37.0
17+
features: "zeroize"
1218
steps:
1319
- uses: actions/checkout@v1
1420
- uses: actions-rs/toolchain@v1
@@ -20,14 +26,16 @@ jobs:
2026
- uses: actions-rs/cargo@v1
2127
with:
2228
command: check
29+
args: --features "${{ matrix.features }}" --no-default-features
2330
- uses: actions-rs/cargo@v1
2431
with:
2532
command: test
33+
args: --features "${{ matrix.features }}" --no-default-features
2634
- uses: actions-rs/cargo@v1
2735
with:
2836
command: fmt
2937
args: --all -- --check
3038
- uses: actions-rs/cargo@v1
3139
with:
3240
command: clippy
33-
args: -- -D warnings
41+
args: --features "${{ matrix.features }}" --no-default-features -- -D warnings

src/buffer.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ impl Buffer {
2424
/// assert_eq!(buf.len(), 76);
2525
/// ```
2626
pub fn new(size: usize) -> Self {
27-
Buffer { inner: vec![0; size] }
27+
Buffer {
28+
inner: vec![0; size],
29+
}
2830
}
2931

3032
/// Create a new buffer with its data copied from the slice.
@@ -38,7 +40,9 @@ impl Buffer {
3840
/// assert_eq!(buf.as_slice(), SOME_DATA);
3941
/// ```
4042
pub fn from(data: &[u8]) -> Self {
41-
Buffer { inner: data.to_vec() }
43+
Buffer {
44+
inner: data.to_vec(),
45+
}
4246
}
4347

4448
pub fn len(&self) -> usize {

src/hash.rs

Lines changed: 93 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@
4444
//! [`hash`]: struct.Hash.html#method.hash
4545
//! [`finish`]: struct.Hash.html#method.finish
4646
47-
use crate::{Error, Result};
4847
use crate::buffer::Buffer;
4948
use crate::helpers::{AlgoHandle, Handle};
50-
use winapi::shared::bcrypt::*;
51-
use winapi::shared::minwindef::{DWORD, ULONG, PUCHAR};
49+
use crate::{Error, Result};
5250
use std::ptr::null_mut;
51+
use winapi::shared::bcrypt::*;
52+
use winapi::shared::minwindef::{DWORD, PUCHAR, ULONG};
5353

5454
/// Hashing algorithm identifiers
5555
#[derive(Debug, Clone, Copy, PartialOrd, PartialEq)]
@@ -141,17 +141,19 @@ impl HashAlgorithm {
141141
let mut hash_handle = HashHandle::new();
142142
let mut object = Buffer::new(object_size);
143143
unsafe {
144-
Error::check(
145-
BCryptCreateHash(
146-
self.handle.as_ptr(),
147-
hash_handle.as_mut_ptr(),
148-
object.as_mut_ptr(),
149-
object.len() as ULONG,
150-
null_mut(),
151-
0,
152-
0
153-
)
154-
).map(|_| Hash { handle: hash_handle, _object: object })
144+
Error::check(BCryptCreateHash(
145+
self.handle.as_ptr(),
146+
hash_handle.as_mut_ptr(),
147+
object.as_mut_ptr(),
148+
object.len() as ULONG,
149+
null_mut(),
150+
0,
151+
0,
152+
))
153+
.map(|_| Hash {
154+
handle: hash_handle,
155+
_object: object,
156+
})
155157
}
156158
}
157159
}
@@ -169,7 +171,9 @@ impl HashHandle {
169171
impl Drop for HashHandle {
170172
fn drop(&mut self) {
171173
if !self.handle.is_null() {
172-
unsafe { BCryptDestroyHash(self.handle); }
174+
unsafe {
175+
BCryptDestroyHash(self.handle);
176+
}
173177
}
174178
}
175179
}
@@ -212,7 +216,7 @@ impl Hash {
212216
self.handle.as_ptr(),
213217
data.as_ptr() as PUCHAR,
214218
data.len() as ULONG,
215-
0
219+
0,
216220
))
217221
}
218222
}
@@ -242,14 +246,13 @@ impl Hash {
242246
let mut result = Buffer::new(hash_size);
243247

244248
unsafe {
245-
Error::check(
246-
BCryptFinishHash(
247-
self.handle.as_ptr(),
248-
result.as_mut_ptr(),
249-
result.len() as ULONG,
250-
0
251-
)
252-
).map(|_| result)
249+
Error::check(BCryptFinishHash(
250+
self.handle.as_ptr(),
251+
result.as_mut_ptr(),
252+
result.len() as ULONG,
253+
0,
254+
))
255+
.map(|_| result)
253256
}
254257
}
255258

@@ -266,7 +269,9 @@ impl Hash {
266269
/// assert_eq!(hash_size, 32);
267270
/// ```
268271
pub fn hash_size(&self) -> Result<usize> {
269-
self.handle.get_property::<DWORD>(BCRYPT_HASH_LENGTH).map(|hash_size| hash_size as usize)
272+
self.handle
273+
.get_property::<DWORD>(BCRYPT_HASH_LENGTH)
274+
.map(|hash_size| hash_size as usize)
270275
}
271276
}
272277

@@ -278,71 +283,92 @@ mod tests {
278283

279284
#[test]
280285
fn sha1() {
281-
check_hash(HashAlgorithmId::Sha1, DATA.as_bytes(), &[
282-
0x2B, 0x44, 0x89, 0x60, 0x6A, 0x23, 0xFB, 0x31,
283-
0xFC, 0xDC, 0x84, 0x9F, 0xA7, 0xE5, 0x77, 0xBA,
284-
0x90, 0xF6, 0xD3, 0x9A,
285-
])
286+
check_hash(
287+
HashAlgorithmId::Sha1,
288+
DATA.as_bytes(),
289+
&[
290+
0x2B, 0x44, 0x89, 0x60, 0x6A, 0x23, 0xFB, 0x31, 0xFC, 0xDC, 0x84, 0x9F, 0xA7, 0xE5,
291+
0x77, 0xBA, 0x90, 0xF6, 0xD3, 0x9A,
292+
],
293+
)
286294
}
287295

288296
#[test]
289297
fn sha256() {
290-
check_hash(HashAlgorithmId::Sha256, DATA.as_bytes(), &[
291-
0x0E, 0xA3, 0x7C, 0x24, 0x3F, 0x60, 0x97, 0x4B,
292-
0x0D, 0x54, 0xC6, 0xB2, 0xD7, 0x6C, 0xEC, 0xE3,
293-
0xF4, 0xC7, 0x42, 0x49, 0x2C, 0xCE, 0x48, 0xEA,
294-
0xF8, 0x1F, 0x35, 0x79, 0x31, 0xD6, 0xD6, 0x9E,
295-
])
298+
check_hash(
299+
HashAlgorithmId::Sha256,
300+
DATA.as_bytes(),
301+
&[
302+
0x0E, 0xA3, 0x7C, 0x24, 0x3F, 0x60, 0x97, 0x4B, 0x0D, 0x54, 0xC6, 0xB2, 0xD7, 0x6C,
303+
0xEC, 0xE3, 0xF4, 0xC7, 0x42, 0x49, 0x2C, 0xCE, 0x48, 0xEA, 0xF8, 0x1F, 0x35, 0x79,
304+
0x31, 0xD6, 0xD6, 0x9E,
305+
],
306+
)
296307
}
297308

298309
#[test]
299310
fn sha384() {
300-
check_hash(HashAlgorithmId::Sha384, DATA.as_bytes(), &[
301-
0x2A, 0x10, 0x60, 0x89, 0x6A, 0xCB, 0xA9, 0xFA,
302-
0x37, 0x11, 0xBF, 0x10, 0x9E, 0x90, 0x24, 0xEA,
303-
0x19, 0xF5, 0xFC, 0x33, 0xAF, 0x0F, 0x47, 0x15,
304-
0xC3, 0xE9, 0xD8, 0x63, 0xB3, 0x24, 0xA5, 0x08,
305-
0x9F, 0xAB, 0x95, 0x36, 0xB2, 0xAC, 0x10, 0xF6,
306-
0xC1, 0xE7, 0x31, 0x03, 0x09, 0x54, 0x18, 0x41,
307-
])
311+
check_hash(
312+
HashAlgorithmId::Sha384,
313+
DATA.as_bytes(),
314+
&[
315+
0x2A, 0x10, 0x60, 0x89, 0x6A, 0xCB, 0xA9, 0xFA, 0x37, 0x11, 0xBF, 0x10, 0x9E, 0x90,
316+
0x24, 0xEA, 0x19, 0xF5, 0xFC, 0x33, 0xAF, 0x0F, 0x47, 0x15, 0xC3, 0xE9, 0xD8, 0x63,
317+
0xB3, 0x24, 0xA5, 0x08, 0x9F, 0xAB, 0x95, 0x36, 0xB2, 0xAC, 0x10, 0xF6, 0xC1, 0xE7,
318+
0x31, 0x03, 0x09, 0x54, 0x18, 0x41,
319+
],
320+
)
308321
}
309322

310323
#[test]
311324
fn sha512() {
312-
check_hash(HashAlgorithmId::Sha512, DATA.as_bytes(), &[
313-
0x39, 0x50, 0xAC, 0xCD, 0xFE, 0xF7, 0x46, 0x20,
314-
0x71, 0x42, 0x78, 0x76, 0x5B, 0xBD, 0xCE, 0x04,
315-
0xD4, 0x57, 0x90, 0x4B, 0x7C, 0xEA, 0x86, 0x31,
316-
0x39, 0x6C, 0xBA, 0x6D, 0x8B, 0xCE, 0xFC, 0xE0,
317-
0x30, 0x8F, 0xC4, 0x7C, 0xFB, 0x88, 0x5B, 0xC8,
318-
0x9E, 0xBD, 0xF4, 0xFF, 0xA6, 0xF9, 0x8F, 0xC8,
319-
0x51, 0x05, 0x54, 0x7C, 0xBD, 0xDF, 0x56, 0x57,
320-
0xB6, 0xAD, 0xBD, 0xDD, 0xA3, 0x8C, 0xB9, 0xB5,
321-
])
325+
check_hash(
326+
HashAlgorithmId::Sha512,
327+
DATA.as_bytes(),
328+
&[
329+
0x39, 0x50, 0xAC, 0xCD, 0xFE, 0xF7, 0x46, 0x20, 0x71, 0x42, 0x78, 0x76, 0x5B, 0xBD,
330+
0xCE, 0x04, 0xD4, 0x57, 0x90, 0x4B, 0x7C, 0xEA, 0x86, 0x31, 0x39, 0x6C, 0xBA, 0x6D,
331+
0x8B, 0xCE, 0xFC, 0xE0, 0x30, 0x8F, 0xC4, 0x7C, 0xFB, 0x88, 0x5B, 0xC8, 0x9E, 0xBD,
332+
0xF4, 0xFF, 0xA6, 0xF9, 0x8F, 0xC8, 0x51, 0x05, 0x54, 0x7C, 0xBD, 0xDF, 0x56, 0x57,
333+
0xB6, 0xAD, 0xBD, 0xDD, 0xA3, 0x8C, 0xB9, 0xB5,
334+
],
335+
)
322336
}
323337

324338
#[test]
325339
fn md2() {
326-
check_hash(HashAlgorithmId::Md2, DATA.as_bytes(), &[
327-
0x08, 0x18, 0x53, 0xA0, 0x5C, 0x1F, 0x58, 0xC6,
328-
0xED, 0x43, 0x46, 0x4C, 0x79, 0x7D, 0x65, 0x26,
329-
])
340+
check_hash(
341+
HashAlgorithmId::Md2,
342+
DATA.as_bytes(),
343+
&[
344+
0x08, 0x18, 0x53, 0xA0, 0x5C, 0x1F, 0x58, 0xC6, 0xED, 0x43, 0x46, 0x4C, 0x79, 0x7D,
345+
0x65, 0x26,
346+
],
347+
)
330348
}
331349

332350
#[test]
333351
fn md4() {
334-
check_hash(HashAlgorithmId::Md4, DATA.as_bytes(), &[
335-
0x24, 0x3C, 0xDA, 0xF5, 0x91, 0x4A, 0xE8, 0x70,
336-
0x91, 0xC7, 0x13, 0xB5, 0xFA, 0x9F, 0xA7, 0x98,
337-
])
352+
check_hash(
353+
HashAlgorithmId::Md4,
354+
DATA.as_bytes(),
355+
&[
356+
0x24, 0x3C, 0xDA, 0xF5, 0x91, 0x4A, 0xE8, 0x70, 0x91, 0xC7, 0x13, 0xB5, 0xFA, 0x9F,
357+
0xA7, 0x98,
358+
],
359+
)
338360
}
339361

340362
#[test]
341363
fn md5() {
342-
check_hash(HashAlgorithmId::Md5, DATA.as_bytes(), &[
343-
0xE8, 0x89, 0xD8, 0x2D, 0xD1, 0x11, 0xD6, 0x31,
344-
0x5D, 0x7B, 0x1E, 0xDC, 0xE2, 0xB1, 0xB3, 0x0F,
345-
])
364+
check_hash(
365+
HashAlgorithmId::Md5,
366+
DATA.as_bytes(),
367+
&[
368+
0xE8, 0x89, 0xD8, 0x2D, 0xD1, 0x11, 0xD6, 0x31, 0x5D, 0x7B, 0x1E, 0xDC, 0xE2, 0xB1,
369+
0xB3, 0x0F,
370+
],
371+
)
346372
}
347373

348374
fn check_hash(algo_id: HashAlgorithmId, data: &[u8], expected_hash: &[u8]) {
@@ -355,4 +381,4 @@ mod tests {
355381
assert_eq!(hash_size, expected_hash.len());
356382
assert_eq!(result.as_slice(), expected_hash);
357383
}
358-
}
384+
}

0 commit comments

Comments
 (0)