Skip to content

Commit 0ac5362

Browse files
committed
Auto merge of #4542 - equal-l2:sha256-crypto-hash, r=alexcrichton
Use crypto-hash to calculate SHA256 `crypto-hash` is an abstraction library for native hash libraries. It uses CryptoAPI on Windows, CommonCrypto on macOS, and OpenSSL on *nix. This PR will also remove `openssl` and `advapi32-sys` from dependencies since they are only used for calculating SHA256, which is superseded by `crypto-hash`. (`crypto-hash` itself uses `openssl` and `advapi32-sys` though)
2 parents 41d3fdb + 8346104 commit 0ac5362

File tree

2 files changed

+16
-96
lines changed

2 files changed

+16
-96
lines changed

Cargo.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ path = "src/cargo/lib.rs"
2020
atty = "0.2"
2121
crates-io = { path = "src/crates-io", version = "0.12" }
2222
crossbeam = "0.3"
23+
crypto-hash = "0.3"
2324
curl = "0.4.6"
2425
docopt = "0.8.1"
2526
env_logger = "0.4"
@@ -52,14 +53,10 @@ termcolor = "0.3"
5253
toml = "0.4"
5354
url = "1.1"
5455

55-
[target.'cfg(unix)'.dependencies]
56-
openssl = "0.9"
57-
5856
[target.'cfg(target_os = "macos")'.dependencies]
5957
core-foundation = { version = "0.4.4", features = ["mac_os_10_7_support"] }
6058

6159
[target.'cfg(windows)'.dependencies]
62-
advapi32-sys = "0.2"
6360
kernel32-sys = "0.2"
6461
miow = "0.2"
6562
psapi-sys = "0.1"

src/cargo/util/sha256.rs

Lines changed: 15 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,23 @@
1-
pub use self::imp::Sha256;
1+
extern crate crypto_hash;
2+
use self::crypto_hash::{Hasher,Algorithm};
3+
use std::io::Write;
24

3-
// Someone upstream will link to OpenSSL, so we don't need to explicitly
4-
// link to it ourselves. Hence we pick up Sha256 digests from OpenSSL
5-
#[cfg(not(windows))]
6-
mod imp {
7-
extern crate openssl;
5+
pub struct Sha256(Hasher);
86

9-
use std::io::Write;
10-
use self::openssl::hash::{Hasher, MessageDigest};
11-
12-
pub struct Sha256(Hasher);
13-
14-
impl Sha256 {
15-
pub fn new() -> Sha256 {
16-
let hasher = Hasher::new(MessageDigest::sha256()).unwrap();
17-
Sha256(hasher)
18-
}
19-
20-
pub fn update(&mut self, bytes: &[u8]) {
21-
let _ = self.0.write_all(bytes);
22-
}
23-
24-
pub fn finish(&mut self) -> [u8; 32] {
25-
let mut ret = [0u8; 32];
26-
let data = self.0.finish2().unwrap();
27-
ret.copy_from_slice(&data[..]);
28-
ret
29-
}
30-
}
31-
}
32-
33-
// Leverage the crypto APIs that windows has built in.
34-
#[cfg(windows)]
35-
mod imp {
36-
extern crate winapi;
37-
extern crate advapi32;
38-
use std::io;
39-
use std::ptr;
40-
41-
use self::winapi::{DWORD, HCRYPTPROV, HCRYPTHASH};
42-
use self::winapi::{PROV_RSA_AES, CRYPT_SILENT, CRYPT_VERIFYCONTEXT, CALG_SHA_256, HP_HASHVAL};
43-
use self::advapi32::{CryptAcquireContextW, CryptCreateHash, CryptDestroyHash};
44-
use self::advapi32::{CryptGetHashParam, CryptHashData, CryptReleaseContext};
45-
46-
macro_rules! call{ ($e:expr) => ({
47-
if $e == 0 {
48-
panic!("failed {}: {}", stringify!($e), io::Error::last_os_error())
49-
}
50-
}) }
51-
52-
pub struct Sha256 {
53-
hcryptprov: HCRYPTPROV,
54-
hcrypthash: HCRYPTHASH,
7+
impl Sha256 {
8+
pub fn new() -> Sha256 {
9+
let hasher = Hasher::new(Algorithm::SHA256);
10+
Sha256(hasher)
5511
}
5612

57-
impl Sha256 {
58-
pub fn new() -> Sha256 {
59-
let mut hcp = 0;
60-
call!(unsafe {
61-
CryptAcquireContextW(&mut hcp, ptr::null(), ptr::null(),
62-
PROV_RSA_AES,
63-
CRYPT_VERIFYCONTEXT | CRYPT_SILENT)
64-
});
65-
let mut ret = Sha256 { hcryptprov: hcp, hcrypthash: 0 };
66-
call!(unsafe {
67-
CryptCreateHash(ret.hcryptprov, CALG_SHA_256,
68-
0, 0, &mut ret.hcrypthash)
69-
});
70-
ret
71-
}
72-
73-
pub fn update(&mut self, bytes: &[u8]) {
74-
call!(unsafe {
75-
CryptHashData(self.hcrypthash, bytes.as_ptr() as *mut _,
76-
bytes.len() as DWORD, 0)
77-
})
78-
}
79-
80-
pub fn finish(&mut self) -> [u8; 32] {
81-
let mut ret = [0u8; 32];
82-
let mut len = ret.len() as DWORD;
83-
call!(unsafe {
84-
CryptGetHashParam(self.hcrypthash, HP_HASHVAL, ret.as_mut_ptr(),
85-
&mut len, 0)
86-
});
87-
assert_eq!(len as usize, ret.len());
88-
ret
89-
}
13+
pub fn update(&mut self, bytes: &[u8]) {
14+
let _ = self.0.write_all(bytes);
9015
}
9116

92-
impl Drop for Sha256 {
93-
fn drop(&mut self) {
94-
if self.hcrypthash != 0 {
95-
call!(unsafe { CryptDestroyHash(self.hcrypthash) });
96-
}
97-
call!(unsafe { CryptReleaseContext(self.hcryptprov, 0) });
98-
}
17+
pub fn finish(&mut self) -> [u8; 32] {
18+
let mut ret = [0u8; 32];
19+
let data = self.0.finish();
20+
ret.copy_from_slice(&data[..]);
21+
ret
9922
}
10023
}

0 commit comments

Comments
 (0)