summaryrefslogtreecommitdiff
path: root/ext/node/crypto/primes.rs
diff options
context:
space:
mode:
authorDivy Srivastava <dj.srivastava23@gmail.com>2023-04-19 22:27:34 +0530
committerGitHub <noreply@github.com>2023-04-19 22:27:34 +0530
commit9496dfc68558a0d6e9fa0a3bf1fbde9883a88d07 (patch)
treefd37f0f325e288c7cf4a48426481d8553c38f6ef /ext/node/crypto/primes.rs
parent53c9f5918cd07237c20b086945d4604baf1900fb (diff)
fix(ext/node): implement asymmetric keygen (#18651)
Towards #18455 This commit implements the keypair generation for asymmetric keys for the `generateKeyPair` API. See how key material is managed in this implementation: https://www.notion.so/denolandinc/node-crypto-design-99fc33f568d24e47a5e4b36002c5325d?pvs=4 Private and public key encoding depend on `KeyObject#export` which is not implemented. I've also skipped ED448 and X448 since we need a crate for that in WebCrypto too.
Diffstat (limited to 'ext/node/crypto/primes.rs')
-rw-r--r--ext/node/crypto/primes.rs25
1 files changed, 25 insertions, 0 deletions
diff --git a/ext/node/crypto/primes.rs b/ext/node/crypto/primes.rs
index f438d8725..d03398f02 100644
--- a/ext/node/crypto/primes.rs
+++ b/ext/node/crypto/primes.rs
@@ -1,10 +1,35 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use num_bigint::BigInt;
+use num_bigint_dig::RandPrime;
use num_integer::Integer;
use num_traits::One;
use num_traits::Zero;
use rand::Rng;
+use std::ops::Deref;
+
+pub struct Prime(num_bigint_dig::BigUint);
+
+impl Prime {
+ pub fn generate(n: usize) -> Self {
+ let mut rng = rand::thread_rng();
+ Self(rng.gen_prime(n))
+ }
+}
+
+impl From<&[u8]> for Prime {
+ fn from(value: &[u8]) -> Self {
+ Self(num_bigint_dig::BigUint::from_bytes_be(value))
+ }
+}
+
+impl Deref for Prime {
+ type Target = num_bigint_dig::BigUint;
+
+ fn deref(&self) -> &Self::Target {
+ &self.0
+ }
+}
struct Witness {
pow: BigInt,