Last active
May 28, 2023 14:43
-
-
Save sapristi/42e9f915acdf3abfa43f5ede1851d568 to your computer and use it in GitHub Desktop.
Rust / Node RSA from bip39 mnemonc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "rust" | |
version = "0.1.0" | |
edition = "2021" | |
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | |
[dependencies] | |
rand = "0.8.5" | |
ring = "0.16.20" | |
rsa = "0.8.2" | |
# tiny-bip39 = "1.0.0" | |
bip39 = "2.0.0" | |
rand_chacha = "0.3.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { mnemonicToSeed } from "bip39"; | |
import forge from "node-forge" | |
import chacha from "chacha" | |
const { pki, random } = forge | |
class ChaChaRng{ | |
constructor(seedBytes) { | |
if (seedBytes instanceof Array || seedBytes instanceof Uint8Array) { | |
seedBytes = new Buffer.from(seedBytes); | |
} | |
if (!(seedBytes instanceof Buffer)) { | |
throw new Error("Seed is not an array"); | |
return; | |
} | |
if (seedBytes.length != 32) { | |
throw new Error("Seed is not 32 bytes long"); | |
return; | |
} | |
let nonce = new Buffer.from(new Uint8Array(12)); | |
this.cipher = chacha.chacha20(seedBytes, nonce); | |
} | |
getBytesSync(n) { | |
const output = this.cipher.chacha.getBytes(n) | |
console.log("GENERATED", n, output) | |
return new Buffer(output).toString("ascii") | |
} | |
} | |
let mnemonic_phrase = "aisle addict reveal track advance assume lock similar system fragile police name surface credit grit"; | |
const seed64 = (await mnemonicToSeed(mnemonic_phrase)) | |
const seed32 = seed64.slice(0, 32) | |
// console.log("SEED64", seed64.length, seed64.toString('hex')) | |
console.log("SEED32", seed32.length, seed32.toString("hex")) | |
const prng = new ChaChaRng(seed32) | |
const { privateKey, publicKey } = pki.rsa.generateKeyPair({ bits: 512, prng, workers: 2 }) | |
console.log("public", pki.publicKeyToPem(publicKey) ) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate ring; | |
extern crate bip39; | |
extern crate rand; | |
use rsa::{ RsaPrivateKey, RsaPublicKey, pkcs8::EncodePublicKey, pkcs8::LineEnding }; | |
use bip39::{Mnemonic}; | |
use rand_chacha::{ChaCha20Rng}; | |
use crate::rand::{SeedableRng,RngCore}; | |
fn main() { | |
let mnemonic_phrase = "aisle addict reveal track advance assume lock similar system fragile police name surface credit grit"; | |
// Convert the mnemonic phrase to a seed | |
let mnemonic = Mnemonic::parse(mnemonic_phrase).expect("Invalid mnemonic phrase"); | |
let seed64 = Mnemonic::to_seed_normalized(&mnemonic, ""); | |
let mut seed32 = [0; 32]; | |
seed32[..32].copy_from_slice(&seed64[..32]); | |
// println!("SEED64 {:02X?}", seed64); | |
println!("SEED32 {:02X?}", seed32); | |
let mut rng = ChaCha20Rng::from_seed(seed32); | |
// Test number generation | |
// let mut testArray33 = [0; 33]; | |
// RngCore::fill_bytes(&mut rng, &mut testArray33); | |
// println!("GENERATED: {:?}", testArray33); | |
let private_key = RsaPrivateKey::new(&mut rng, 512).expect("failed to generate a key"); | |
let public_key = RsaPublicKey::from(&private_key); | |
let public_der = RsaPublicKey::to_public_key_pem(&public_key, LineEnding::LF).expect("Problem"); | |
// Print the public key and private key in DER format | |
println!("RSA Public Key (DER): {:?}", public_der); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"dependencies": { | |
"bip39": "^3.1.0", | |
"node-forge": "^1.3.1", | |
"chacha": "^2.1.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment