From b0264bea7de1901c1b3ed764454290d10613d14b Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Thu, 27 Apr 2023 19:40:59 +0530 Subject: fix(ext/node): prime generation (#18861) Towards https://github.com/denoland/deno/issues/18455 `safe`, `add` and `rem` options are not implemented because there is no rust crate that provides this functionality (except rust-openssl maybe) and its just not clear if this API is used widely. --- ext/node/ops/crypto/mod.rs | 17 +++++++++++++++++ ext/node/ops/crypto/primes.rs | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) (limited to 'ext/node/ops') diff --git a/ext/node/ops/crypto/mod.rs b/ext/node/ops/crypto/mod.rs index d224b40f7..92e3029e0 100644 --- a/ext/node/ops/crypto/mod.rs +++ b/ext/node/ops/crypto/mod.rs @@ -901,3 +901,20 @@ pub async fn op_node_scrypt_async( }) .await? } + +#[inline] +fn gen_prime(size: usize) -> ZeroCopyBuf { + primes::Prime::generate(size).0.to_bytes_be().into() +} + +#[op] +pub fn op_node_gen_prime(size: usize) -> ZeroCopyBuf { + gen_prime(size) +} + +#[op] +pub async fn op_node_gen_prime_async( + size: usize, +) -> Result { + Ok(tokio::task::spawn_blocking(move || gen_prime(size)).await?) +} diff --git a/ext/node/ops/crypto/primes.rs b/ext/node/ops/crypto/primes.rs index d03398f02..15aa643ad 100644 --- a/ext/node/ops/crypto/primes.rs +++ b/ext/node/ops/crypto/primes.rs @@ -8,7 +8,7 @@ use num_traits::Zero; use rand::Rng; use std::ops::Deref; -pub struct Prime(num_bigint_dig::BigUint); +pub struct Prime(pub num_bigint_dig::BigUint); impl Prime { pub fn generate(n: usize) -> Self { -- cgit v1.2.3