summaryrefslogtreecommitdiff
path: root/extensions/crypto/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'extensions/crypto/lib.rs')
-rw-r--r--extensions/crypto/lib.rs29
1 files changed, 29 insertions, 0 deletions
diff --git a/extensions/crypto/lib.rs b/extensions/crypto/lib.rs
index 0ec7f4717..9d71cc761 100644
--- a/extensions/crypto/lib.rs
+++ b/extensions/crypto/lib.rs
@@ -1,7 +1,9 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
+use deno_core::error::null_opbuf;
use deno_core::error::AnyError;
use deno_core::include_js_files;
+use deno_core::op_async;
use deno_core::op_sync;
use deno_core::Extension;
use deno_core::OpState;
@@ -10,7 +12,10 @@ use rand::rngs::StdRng;
use rand::thread_rng;
use rand::Rng;
use rand::SeedableRng;
+use ring::digest;
+use std::cell::RefCell;
use std::path::PathBuf;
+use std::rc::Rc;
pub use rand; // Re-export rand
@@ -18,6 +23,7 @@ pub fn init(maybe_seed: Option<u64>) -> Extension {
Extension::builder()
.js(include_js_files!(
prefix "deno:extensions/crypto",
+ "00_webidl.js",
"01_crypto.js",
))
.ops(vec![
@@ -25,6 +31,7 @@ pub fn init(maybe_seed: Option<u64>) -> Extension {
"op_crypto_get_random_values",
op_sync(op_crypto_get_random_values),
),
+ ("op_crypto_subtle_digest", op_async(op_crypto_subtle_digest)),
("op_crypto_random_uuid", op_sync(op_crypto_random_uuid)),
])
.state(move |state| {
@@ -78,6 +85,28 @@ pub fn op_crypto_random_uuid(
Ok(uuid.to_string())
}
+pub async fn op_crypto_subtle_digest(
+ _state: Rc<RefCell<OpState>>,
+ algorithm_id: i8,
+ 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()
+ })
+ .await?;
+
+ Ok(output)
+}
+
pub fn get_declaration() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("lib.deno_crypto.d.ts")
}