diff options
| author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2023-02-14 17:38:45 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-02-14 17:38:45 +0100 |
| commit | d47147fb6ad229b1c039aff9d0959b6e281f4df5 (patch) | |
| tree | 6e9e790f2b9bc71b5f0c9c7e64b95cae31579d58 /ext/node/crypto | |
| parent | 1d00bbe47e2ca14e2d2151518e02b2324461a065 (diff) | |
feat(ext/node): embed std/node into the snapshot (#17724)
This commit moves "deno_std/node" in "ext/node" crate. The code is
transpiled and snapshotted during the build process.
During the first pass a minimal amount of work was done to create the
snapshot, a lot of code in "ext/node" depends on presence of "Deno"
global. This code will be gradually fixed in the follow up PRs to migrate
it to import relevant APIs from "internal:" modules.
Currently the code from snapshot is not used in any way, and all
Node/npm compatibility still uses code from
"https://deno.land/std/node" (or from the location specified by
"DENO_NODE_COMPAT_URL"). This will also be handled in a follow
up PRs.
---------
Co-authored-by: crowlkats <crowlkats@toaxl.com>
Co-authored-by: Divy Srivastava <dj.srivastava23@gmail.com>
Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
Diffstat (limited to 'ext/node/crypto')
| -rw-r--r-- | ext/node/crypto/digest.rs | 117 | ||||
| -rw-r--r-- | ext/node/crypto/mod.rs | 49 |
2 files changed, 166 insertions, 0 deletions
diff --git a/ext/node/crypto/digest.rs b/ext/node/crypto/digest.rs new file mode 100644 index 000000000..4fab58a43 --- /dev/null +++ b/ext/node/crypto/digest.rs @@ -0,0 +1,117 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +use deno_core::error::type_error; +use deno_core::error::AnyError; +use deno_core::Resource; +use digest::Digest; +use digest::DynDigest; +use std::borrow::Cow; +use std::cell::RefCell; +use std::rc::Rc; + +pub enum Hash { + Md4(Box<md4::Md4>), + Md5(Box<md5::Md5>), + Ripemd160(Box<ripemd::Ripemd160>), + Sha1(Box<sha1::Sha1>), + Sha224(Box<sha2::Sha224>), + Sha256(Box<sha2::Sha256>), + Sha384(Box<sha2::Sha384>), + Sha512(Box<sha2::Sha512>), +} + +pub struct Context { + pub hash: Rc<RefCell<Hash>>, +} + +impl Context { + pub fn new(algorithm: &str) -> Result<Self, AnyError> { + Ok(Self { + hash: Rc::new(RefCell::new(Hash::new(algorithm)?)), + }) + } + + pub fn update(&self, data: &[u8]) { + self.hash.borrow_mut().update(data); + } + + pub fn digest(self) -> Result<Box<[u8]>, AnyError> { + let hash = Rc::try_unwrap(self.hash) + .map_err(|_| type_error("Hash context is already in use"))?; + + let hash = hash.into_inner(); + Ok(hash.digest_and_drop()) + } +} + +impl Clone for Context { + fn clone(&self) -> Self { + Self { + hash: Rc::new(RefCell::new(self.hash.borrow().clone())), + } + } +} + +impl Resource for Context { + fn name(&self) -> Cow<str> { + "cryptoDigest".into() + } +} + +use Hash::*; + +impl Hash { + pub fn new(algorithm_name: &str) -> Result<Self, AnyError> { + Ok(match algorithm_name { + "md4" => Md4(Default::default()), + "md5" => Md5(Default::default()), + "ripemd160" => Ripemd160(Default::default()), + "sha1" => Sha1(Default::default()), + "sha224" => Sha224(Default::default()), + "sha256" => Sha256(Default::default()), + "sha384" => Sha384(Default::default()), + "sha512" => Sha512(Default::default()), + _ => return Err(type_error("unsupported algorithm")), + }) + } + + pub fn update(&mut self, data: &[u8]) { + match self { + Md4(context) => Digest::update(&mut **context, data), + Md5(context) => Digest::update(&mut **context, data), + Ripemd160(context) => Digest::update(&mut **context, data), + Sha1(context) => Digest::update(&mut **context, data), + Sha224(context) => Digest::update(&mut **context, data), + Sha256(context) => Digest::update(&mut **context, data), + Sha384(context) => Digest::update(&mut **context, data), + Sha512(context) => Digest::update(&mut **context, data), + }; + } + + pub fn digest_and_drop(self) -> Box<[u8]> { + match self { + Md4(context) => context.finalize(), + Md5(context) => context.finalize(), + Ripemd160(context) => context.finalize(), + Sha1(context) => context.finalize(), + Sha224(context) => context.finalize(), + Sha256(context) => context.finalize(), + Sha384(context) => context.finalize(), + Sha512(context) => context.finalize(), + } + } +} + +impl Clone for Hash { + fn clone(&self) -> Self { + match self { + Md4(_) => Md4(Default::default()), + Md5(_) => Md5(Default::default()), + Ripemd160(_) => Ripemd160(Default::default()), + Sha1(_) => Sha1(Default::default()), + Sha224(_) => Sha224(Default::default()), + Sha256(_) => Sha256(Default::default()), + Sha384(_) => Sha384(Default::default()), + Sha512(_) => Sha512(Default::default()), + } + } +} diff --git a/ext/node/crypto/mod.rs b/ext/node/crypto/mod.rs new file mode 100644 index 000000000..be92dc7ef --- /dev/null +++ b/ext/node/crypto/mod.rs @@ -0,0 +1,49 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +use deno_core::error::type_error; +use deno_core::error::AnyError; +use deno_core::op; +use deno_core::OpState; +use deno_core::ResourceId; +use deno_core::ZeroCopyBuf; +use std::rc::Rc; + +mod digest; + +#[op] +pub fn op_node_create_hash( + state: &mut OpState, + algorithm: String, +) -> Result<ResourceId, AnyError> { + Ok(state.resource_table.add(digest::Context::new(&algorithm)?)) +} + +#[op] +pub fn op_node_hash_update( + state: &mut OpState, + rid: ResourceId, + data: &[u8], +) -> Result<(), AnyError> { + let context = state.resource_table.get::<digest::Context>(rid)?; + context.update(data); + Ok(()) +} + +#[op] +pub fn op_node_hash_digest( + state: &mut OpState, + rid: ResourceId, +) -> Result<ZeroCopyBuf, AnyError> { + let context = state.resource_table.take::<digest::Context>(rid)?; + let context = Rc::try_unwrap(context) + .map_err(|_| type_error("Hash context is already in use"))?; + Ok(context.digest()?.into()) +} + +#[op] +pub fn op_node_hash_clone( + state: &mut OpState, + rid: ResourceId, +) -> Result<ResourceId, AnyError> { + let context = state.resource_table.get::<digest::Context>(rid)?; + Ok(state.resource_table.add(context.as_ref().clone())) +} |
