refactor(extensions/crypto): use key::CryptoHash with digest (#11309)

This commit is contained in:
Divy Srivastava 2021-07-08 00:04:02 +05:30 committed by GitHub
parent 48e7c871d9
commit 5bcbbb75e9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 25 deletions

View file

@ -109,20 +109,6 @@
return normalizedAlgorithm;
}
// Should match op_crypto_subtle_digest() in extensions/crypto/lib.rs
function digestToId(name) {
switch (name) {
case "SHA-1":
return 0;
case "SHA-256":
return 1;
case "SHA-384":
return 2;
case "SHA-512":
return 3;
}
}
const _handle = Symbol("[[handle]]");
const _algorithm = Symbol("[[algorithm]]");
const _extractable = Symbol("[[extractable]]");
@ -256,7 +242,7 @@
const result = await core.opAsync(
"op_crypto_subtle_digest",
digestToId(algorithm.name),
algorithm.name,
data,
);

View file

@ -1,6 +1,7 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
use ring::agreement::Algorithm as RingAlgorithm;
use ring::digest;
use ring::hmac::Algorithm as HmacAlgorithm;
use ring::signature::EcdsaSigningAlgorithm;
use serde::Deserialize;
@ -67,6 +68,17 @@ impl From<CryptoHash> for HmacAlgorithm {
}
}
impl From<CryptoHash> for &'static digest::Algorithm {
fn from(hash: CryptoHash) -> &'static digest::Algorithm {
match hash {
CryptoHash::Sha1 => &digest::SHA1_FOR_LEGACY_USE_ONLY,
CryptoHash::Sha256 => &digest::SHA256,
CryptoHash::Sha384 => &digest::SHA384,
CryptoHash::Sha512 => &digest::SHA512,
}
}
}
#[derive(Serialize, Deserialize, Copy, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum KeyUsage {

View file

@ -399,20 +399,15 @@ pub fn op_crypto_random_uuid(
pub async fn op_crypto_subtle_digest(
_state: Rc<RefCell<OpState>>,
algorithm_id: i8,
algorithm: CryptoHash,
data: Option<ZeroCopyBuf>,
) -> Result<ZeroCopyBuf, AnyError> {
let algorithm = match algorithm_id {
0 => &digest::SHA1_FOR_LEGACY_USE_ONLY,
1 => &digest::SHA256,
2 => &digest::SHA384,
3 => &digest::SHA512,
_ => panic!("Invalid algorithm id"),
};
let input = data.ok_or_else(null_opbuf)?;
let output = tokio::task::spawn_blocking(move || {
digest::digest(algorithm, &input).as_ref().to_vec().into()
digest::digest(algorithm.into(), &input)
.as_ref()
.to_vec()
.into()
})
.await?;