-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
70 lines (58 loc) · 1.83 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// @deno-types="npm:@types/node-forge@^1.3.1"
import forge from 'npm:node-forge@^1.3.1';
/** Get SHA-256 hex digest from a string. */
function getDigest(message: string) {
const md = forge.md.sha256.create();
md.update(message);
return md.digest().toHex();
}
/**
* Convert a string into an ArrayBuffer.
* <https://developers.google.com/web/updates/2012/06/How-to-convert-ArrayBuffer-to-and-from-String>
*/
function str2ab(str: string): ArrayBuffer {
const buf = new ArrayBuffer(str.length);
const bufView = new Uint8Array(buf);
for (let i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
interface GenerateSeededRsaOptions {
bits?: number;
}
/** Generate a deterministic RSA keypair from a seed. */
async function generateSeededRsa(seed: string, opts: GenerateSeededRsaOptions = {}): Promise<CryptoKeyPair> {
// Seed the PRNG with a SHA-256 digest from the string.
const prng = forge.random.createInstance();
prng.seedFileSync = () => getDigest(seed);
const keys = forge.pki.rsa.generateKeyPair({ ...opts, prng });
const rsaPublicKey = forge.pki.publicKeyToAsn1(keys.publicKey);
const publicKeyData = str2ab(forge.asn1.toDer(rsaPublicKey).getBytes());
const rsaPrivateKey = forge.pki.privateKeyToAsn1(keys.privateKey);
const privateKeyInfo = forge.pki.wrapRsaPrivateKey(rsaPrivateKey);
const privateKeyData = str2ab(forge.asn1.toDer(privateKeyInfo).getBytes());
const algorithm = {
name: 'RSASSA-PKCS1-v1_5',
hash: 'SHA-256',
};
const publicKey = await crypto.subtle.importKey(
'spki',
publicKeyData,
algorithm,
true,
['verify'],
);
const privateKey = await crypto.subtle.importKey(
'pkcs8',
privateKeyData,
algorithm,
true,
['sign'],
);
return {
publicKey,
privateKey,
};
}
export { generateSeededRsa };