-
Notifications
You must be signed in to change notification settings - Fork 22
/
private_key.ts
40 lines (31 loc) · 1.22 KB
/
private_key.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
import { AsnParser, AsnSerializer } from "@peculiar/asn1-schema";
import { JsonParser, JsonSerializer } from "@peculiar/json-schema";
import * as asn from "../../asn";
import { AsymmetricKey } from "../../keys";
import { getJwkAlgorithm } from "./helper";
export class RsaPrivateKey extends AsymmetricKey {
public readonly type: "private" = "private";
public algorithm!: RsaHashedKeyAlgorithm;
public getKey() {
const keyInfo = AsnParser.parse(this.data, asn.PrivateKeyInfo);
return AsnParser.parse(keyInfo.privateKey, asn.RsaPrivateKey);
}
public toJSON() {
const key = this.getKey();
const json: JsonWebKey = {
kty: "RSA",
alg: getJwkAlgorithm(this.algorithm),
key_ops: this.usages,
ext: this.extractable,
};
return Object.assign(json, JsonSerializer.toJSON(key));
}
public fromJSON(json: JsonWebKey) {
const key = JsonParser.fromJSON(json, { targetSchema: asn.RsaPrivateKey });
const keyInfo = new asn.PrivateKeyInfo();
keyInfo.privateKeyAlgorithm.algorithm = "1.2.840.113549.1.1.1";
keyInfo.privateKeyAlgorithm.parameters = null;
keyInfo.privateKey = AsnSerializer.serialize(key);
this.data = Buffer.from(AsnSerializer.serialize(keyInfo));
}
}