Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use sha2 to calculate SHA256 #4545

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 73 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,18 @@ serde = "1.0"
serde_derive = "1.0"
serde_ignored = "0.0.4"
serde_json = "1.0"
sha2 = "0.6"
shell-escape = "0.1"
tar = { version = "0.4", default-features = false }
tempdir = "0.3"
termcolor = "0.3"
toml = "0.4"
url = "1.1"

[target.'cfg(unix)'.dependencies]
openssl = "0.9"

[target.'cfg(target_os = "macos")'.dependencies]
core-foundation = { version = "0.4.4", features = ["mac_os_10_7_support"] }

[target.'cfg(windows)'.dependencies]
advapi32-sys = "0.2"
kernel32-sys = "0.2"
miow = "0.2"
psapi-sys = "0.1"
Expand Down
107 changes: 17 additions & 90 deletions src/cargo/util/sha256.rs
Original file line number Diff line number Diff line change
@@ -1,100 +1,27 @@
pub use self::imp::Sha256;
extern crate sha2;
use self::sha2::Digest;

// Someone upstream will link to OpenSSL, so we don't need to explicitly
// link to it ourselves. Hence we pick up Sha256 digests from OpenSSL
#[cfg(not(windows))]
mod imp {
extern crate openssl;
pub struct Sha256(sha2::Sha256);

use std::io::Write;
use self::openssl::hash::{Hasher, MessageDigest};

pub struct Sha256(Hasher);

impl Sha256 {
pub fn new() -> Sha256 {
let hasher = Hasher::new(MessageDigest::sha256()).unwrap();
Sha256(hasher)
}

pub fn update(&mut self, bytes: &[u8]) {
let _ = self.0.write_all(bytes);
}

pub fn finish(&mut self) -> [u8; 32] {
let mut ret = [0u8; 32];
let data = self.0.finish2().unwrap();
ret.copy_from_slice(&data[..]);
ret
}
impl Sha256 {
pub fn new() -> Sha256 {
let hasher = sha2::Sha256::new();
Sha256(hasher)
}
}

// Leverage the crypto APIs that windows has built in.
#[cfg(windows)]
mod imp {
extern crate winapi;
extern crate advapi32;
use std::io;
use std::ptr;

use self::winapi::{DWORD, HCRYPTPROV, HCRYPTHASH};
use self::winapi::{PROV_RSA_AES, CRYPT_SILENT, CRYPT_VERIFYCONTEXT, CALG_SHA_256, HP_HASHVAL};
use self::advapi32::{CryptAcquireContextW, CryptCreateHash, CryptDestroyHash};
use self::advapi32::{CryptGetHashParam, CryptHashData, CryptReleaseContext};

macro_rules! call{ ($e:expr) => ({
if $e == 0 {
panic!("failed {}: {}", stringify!($e), io::Error::last_os_error())
}
}) }

pub struct Sha256 {
hcryptprov: HCRYPTPROV,
hcrypthash: HCRYPTHASH,
pub fn update(&mut self, bytes: &[u8]) {
let _ = self.0.input(bytes);
}

impl Sha256 {
pub fn new() -> Sha256 {
let mut hcp = 0;
call!(unsafe {
CryptAcquireContextW(&mut hcp, ptr::null(), ptr::null(),
PROV_RSA_AES,
CRYPT_VERIFYCONTEXT | CRYPT_SILENT)
});
let mut ret = Sha256 { hcryptprov: hcp, hcrypthash: 0 };
call!(unsafe {
CryptCreateHash(ret.hcryptprov, CALG_SHA_256,
0, 0, &mut ret.hcrypthash)
});
ret
}
pub fn finish(&mut self) -> [u8; 32] {
let mut ret = [0u8; 32];
let data = self.0.result();
ret.copy_from_slice(&data[..]);

pub fn update(&mut self, bytes: &[u8]) {
call!(unsafe {
CryptHashData(self.hcrypthash, bytes.as_ptr() as *mut _,
bytes.len() as DWORD, 0)
})
}

pub fn finish(&mut self) -> [u8; 32] {
let mut ret = [0u8; 32];
let mut len = ret.len() as DWORD;
call!(unsafe {
CryptGetHashParam(self.hcrypthash, HP_HASHVAL, ret.as_mut_ptr(),
&mut len, 0)
});
assert_eq!(len as usize, ret.len());
ret
}
}
// sha2::Sha256::result() doesn't reset its buffer
// Reset manually by replacing hasher
self.0 = sha2::Sha256::new();

impl Drop for Sha256 {
fn drop(&mut self) {
if self.hcrypthash != 0 {
call!(unsafe { CryptDestroyHash(self.hcrypthash) });
}
call!(unsafe { CryptReleaseContext(self.hcryptprov, 0) });
}
ret
}
}