@push.rocks/smartcrypto 2.4.1 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/.smartconfig.json +0 -1
  2. package/dist_ts/00_commitinfo_data.js +1 -1
  3. package/dist_ts/index.d.ts +1 -2
  4. package/dist_ts/index.js +1 -3
  5. package/dist_ts/smartcrypto.classes.keypair.d.ts +1 -0
  6. package/dist_ts/smartcrypto.classes.keypair.js +34 -15
  7. package/dist_ts/smartcrypto.classes.privatekey.d.ts +26 -3
  8. package/dist_ts/smartcrypto.classes.privatekey.js +93 -14
  9. package/dist_ts/smartcrypto.classes.publickey.d.ts +20 -7
  10. package/dist_ts/smartcrypto.classes.publickey.js +59 -45
  11. package/dist_ts/smartcrypto.classes.smartcrypto.js +1 -2
  12. package/dist_ts/smartcrypto.der.d.ts +44 -0
  13. package/dist_ts/smartcrypto.der.js +196 -0
  14. package/dist_ts/smartcrypto.internal.d.ts +1 -0
  15. package/dist_ts/smartcrypto.internal.js +4 -3
  16. package/dist_ts/smartcrypto.openssh.d.ts +8 -0
  17. package/dist_ts/smartcrypto.openssh.js +51 -0
  18. package/dist_ts/smartcrypto.pem.d.ts +16 -0
  19. package/dist_ts/smartcrypto.pem.js +50 -0
  20. package/dist_ts/smartcrypto.pkcs8.d.ts +4 -6
  21. package/dist_ts/smartcrypto.pkcs8.js +33 -95
  22. package/dist_ts/smartcrypto.rsa.d.ts +30 -0
  23. package/dist_ts/smartcrypto.rsa.js +153 -0
  24. package/package.json +3 -7
  25. package/readme.md +63 -9
  26. package/ts/00_commitinfo_data.ts +1 -1
  27. package/ts/index.ts +4 -4
  28. package/ts/smartcrypto.classes.keypair.ts +34 -17
  29. package/ts/smartcrypto.classes.privatekey.ts +111 -19
  30. package/ts/smartcrypto.classes.publickey.ts +83 -78
  31. package/ts/smartcrypto.classes.smartcrypto.ts +0 -1
  32. package/ts/smartcrypto.der.ts +235 -0
  33. package/ts/smartcrypto.internal.ts +5 -2
  34. package/ts/smartcrypto.openssh.ts +98 -0
  35. package/ts/smartcrypto.pem.ts +61 -0
  36. package/ts/smartcrypto.pkcs8.ts +63 -121
  37. package/ts/smartcrypto.rsa.ts +234 -0
  38. package/dist_ts/smartcrypto.plugins.d.ts +0 -4
  39. package/dist_ts/smartcrypto.plugins.js +0 -7
  40. package/ts/smartcrypto.plugins.ts +0 -9
package/ts/index.ts CHANGED
@@ -2,6 +2,10 @@ export * from './smartcrypto.classes.smartcrypto.js';
2
2
  export * from './smartcrypto.classes.keypair.js';
3
3
  export * from './smartcrypto.classes.privatekey.js';
4
4
  export * from './smartcrypto.classes.publickey.js';
5
+ export type {
6
+ IRsaPrivateKeyComponents,
7
+ IRsaPublicKeyComponents,
8
+ } from './smartcrypto.rsa.js';
5
9
  export { SmartCryptoError } from './smartcrypto.errors.js';
6
10
  export type {
7
11
  ISmartCryptoErrorJson,
@@ -10,7 +14,3 @@ export type {
10
14
  export * from './smartcrypto.webcrypto.js';
11
15
  export * from './smartcrypto.x25519.js';
12
16
  export * from './smartcrypto.hash.js';
13
-
14
- import { nodeForge } from './smartcrypto.plugins.js';
15
-
16
- export { nodeForge };
@@ -1,7 +1,8 @@
1
- import * as plugins from './smartcrypto.plugins.js';
2
1
  import { PublicKey } from './smartcrypto.classes.publickey.js';
3
2
  import { PrivateKey } from './smartcrypto.classes.privatekey.js';
4
- import { createSmartCryptoError } from './smartcrypto.errors.js';
3
+ import { SmartCryptoError, createSmartCryptoError } from './smartcrypto.errors.js';
4
+ import { getWebCrypto } from './smartcrypto.internal.js';
5
+ import { decodePrivateKeyInfo, zeroRsaPrivateKeyComponents } from './smartcrypto.rsa.js';
5
6
 
6
7
  export type TRsaModulusLength = 2048 | 3072 | 4096;
7
8
 
@@ -14,27 +15,43 @@ const rsaModulusLengths: readonly TRsaModulusLength[] = [2048, 3072, 4096];
14
15
 
15
16
  export class KeyPair {
16
17
  // STATIC
18
+ /** Generates an RSA key pair with public exponent 65537 via WebCrypto. */
17
19
  public static async createNewKeyPair(optionsArg: IKeyPairOptions = {}): Promise<KeyPair> {
18
20
  const modulusLength = optionsArg.modulusLength ?? 2048;
19
21
  if (!rsaModulusLengths.includes(modulusLength)) {
20
22
  throw createSmartCryptoError('INVALID_ARGUMENT');
21
23
  }
22
- const done = plugins.smartpromise.defer<KeyPair>();
23
- const rsa = plugins.nodeForge.pki.rsa;
24
- rsa.generateKeyPair({ bits: modulusLength, workers: 2 }, (err, keypair) => {
25
- if (err) {
26
- done.reject(createSmartCryptoError('CRYPTO_OPERATION_FAILED'));
27
- return;
28
- }
29
-
30
- done.resolve(
31
- new KeyPair({
32
- privateKey: new PrivateKey(keypair.privateKey),
33
- publicKey: new PublicKey(keypair.publicKey),
34
- })
24
+ const subtle = getWebCrypto().subtle;
25
+ let privateKeyInfo: Uint8Array | undefined;
26
+ try {
27
+ const generatedKeyPair = await subtle.generateKey(
28
+ {
29
+ name: 'RSASSA-PKCS1-v1_5',
30
+ modulusLength,
31
+ publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
32
+ hash: 'SHA-256',
33
+ },
34
+ true,
35
+ ['sign', 'verify'],
35
36
  );
36
- });
37
- return done.promise;
37
+ privateKeyInfo = new Uint8Array(await subtle.exportKey('pkcs8', generatedKeyPair.privateKey));
38
+ const components = decodePrivateKeyInfo(privateKeyInfo);
39
+ try {
40
+ return new KeyPair({
41
+ privateKey: new PrivateKey(components),
42
+ publicKey: new PublicKey(components),
43
+ });
44
+ } finally {
45
+ zeroRsaPrivateKeyComponents(components);
46
+ }
47
+ } catch (error) {
48
+ if (error instanceof SmartCryptoError) {
49
+ throw error;
50
+ }
51
+ throw createSmartCryptoError('CRYPTO_OPERATION_FAILED');
52
+ } finally {
53
+ privateKeyInfo?.fill(0);
54
+ }
38
55
  }
39
56
 
40
57
  // INSTANCE
@@ -1,16 +1,44 @@
1
- import * as plugins from './smartcrypto.plugins.js';
2
- import { createSmartCryptoError } from './smartcrypto.errors.js';
1
+ import { SmartCryptoError, createSmartCryptoError } from './smartcrypto.errors.js';
2
+ import { getWebCrypto, toArrayBuffer } from './smartcrypto.internal.js';
3
+ import { encodeOpenSshRsaPrivateKey } from './smartcrypto.openssh.js';
4
+ import { decodeFirstPemBlock, encodePem } from './smartcrypto.pem.js';
3
5
  import { decryptPrivateKeyInfo, encryptPrivateKeyInfo } from './smartcrypto.pkcs8.js';
6
+ import {
7
+ bitLengthOf,
8
+ copyRsaPrivateKeyComponents,
9
+ decodePrivateKeyInfo,
10
+ decodeRsaPrivateKey,
11
+ encodePrivateKeyInfo,
12
+ encodeRsaPrivateKey,
13
+ type IRsaPrivateKeyComponents,
14
+ } from './smartcrypto.rsa.js';
15
+
16
+ const pkcs1PrivatePemType = 'RSA PRIVATE KEY';
17
+ const pkcs8PrivatePemType = 'PRIVATE KEY';
4
18
 
5
19
  export class PrivateKey {
6
20
  // STATIC
21
+ /** Parses an unencrypted RSA private-key PEM; the same as `fromPemString()`. */
7
22
  public static createFromPrivateKey(pemString: string) {
8
- const privateKey = plugins.nodeForge.pki.privateKeyFromPem(pemString);
9
- return new PrivateKey(privateKey);
23
+ return PrivateKey.fromPemString(pemString);
10
24
  }
11
25
 
26
+ /**
27
+ * Parses the first PEM block of `pemString` as an unencrypted PKCS#1
28
+ * (`RSA PRIVATE KEY`) or PKCS#8 (`PRIVATE KEY`) RSA private key.
29
+ * Fails with `INVALID_KEY`.
30
+ */
12
31
  public static fromPemString(pemString: string) {
13
- return new PrivateKey(plugins.nodeForge.pki.privateKeyFromPem(pemString));
32
+ const pemBlock = decodeFirstPemBlock(pemString, [pkcs1PrivatePemType, pkcs8PrivatePemType]);
33
+ try {
34
+ return new PrivateKey(
35
+ pemBlock.type === pkcs1PrivatePemType
36
+ ? decodeRsaPrivateKey(pemBlock.body)
37
+ : decodePrivateKeyInfo(pemBlock.body),
38
+ );
39
+ } finally {
40
+ pemBlock.body.fill(0);
41
+ }
14
42
  }
15
43
 
16
44
  /**
@@ -25,21 +53,34 @@ export class PrivateKey {
25
53
  ): Promise<PrivateKey> {
26
54
  const privateKeyInfo = await decryptPrivateKeyInfo(pemString, passphraseArg);
27
55
  try {
28
- return new PrivateKey(plugins.nodeForge.pki.privateKeyFromAsn1(privateKeyInfo));
29
- } catch {
30
- throw createSmartCryptoError('INVALID_KEY');
56
+ return new PrivateKey(decodePrivateKeyInfo(privateKeyInfo));
57
+ } finally {
58
+ privateKeyInfo.fill(0);
31
59
  }
32
60
  }
33
61
 
34
62
  // INSTANCE
35
- public forgePrivateKey: plugins.nodeForge.pki.rsa.PrivateKey;
63
+ private readonly components: IRsaPrivateKeyComponents;
64
+ private signingKey?: Promise<CryptoKey>;
36
65
 
37
- constructor(privateKeyArg: plugins.nodeForge.pki.rsa.PrivateKey) {
38
- this.forgePrivateKey = privateKeyArg;
66
+ /** Creates a private key from its RFC 8017 integers. Fails with `INVALID_KEY`. */
67
+ constructor(componentsArg: IRsaPrivateKeyComponents) {
68
+ this.components = copyRsaPrivateKeyComponents(componentsArg);
39
69
  }
40
70
 
71
+ /** The RSA modulus length in bits. */
72
+ public get modulusLength(): number {
73
+ return bitLengthOf(this.components.modulus);
74
+ }
75
+
76
+ /** Writes an unencrypted PKCS#1 `-----BEGIN RSA PRIVATE KEY-----` PEM (64-column lines, CRLF). */
41
77
  public toPemString(): string {
42
- return plugins.nodeForge.pki.privateKeyToPem(this.forgePrivateKey);
78
+ const der = encodeRsaPrivateKey(this.components);
79
+ try {
80
+ return encodePem(pkcs1PrivatePemType, der);
81
+ } finally {
82
+ der.fill(0);
83
+ }
43
84
  }
44
85
 
45
86
  /**
@@ -48,15 +89,66 @@ export class PrivateKey {
48
89
  * PBKDF2-HMAC-SHA256 (600,000 iterations) and AES-256-CBC via WebCrypto.
49
90
  */
50
91
  public async toEncryptedPemString(passphraseArg: string): Promise<string> {
51
- return encryptPrivateKeyInfo(
52
- plugins.nodeForge.pki.wrapRsaPrivateKey(
53
- plugins.nodeForge.pki.privateKeyToAsn1(this.forgePrivateKey),
54
- ),
55
- passphraseArg,
56
- );
92
+ const privateKeyInfo = encodePrivateKeyInfo(this.components);
93
+ try {
94
+ return await encryptPrivateKeyInfo(privateKeyInfo, passphraseArg);
95
+ } finally {
96
+ privateKeyInfo.fill(0);
97
+ }
98
+ }
99
+
100
+ /**
101
+ * Signs `dataArg` with RSASSA-PKCS1-v1_5 over SHA-256 (JWS `RS256`) via
102
+ * WebCrypto and returns the signature bytes.
103
+ */
104
+ public async createRs256Signature(dataArg: Uint8Array): Promise<Uint8Array> {
105
+ if (!(dataArg instanceof Uint8Array)) {
106
+ throw createSmartCryptoError('INVALID_ARGUMENT');
107
+ }
108
+ const subtle = getWebCrypto().subtle;
109
+ const signingKey = await this.getSigningKey();
110
+ try {
111
+ return new Uint8Array(
112
+ await subtle.sign({ name: 'RSASSA-PKCS1-v1_5' }, signingKey, toArrayBuffer(dataArg)),
113
+ );
114
+ } catch {
115
+ throw createSmartCryptoError('CRYPTO_OPERATION_FAILED');
116
+ }
57
117
  }
58
118
 
119
+ /**
120
+ * Writes an unencrypted OpenSSH private key
121
+ * (`-----BEGIN OPENSSH PRIVATE KEY-----`, `openssh-key-v1`) with the comment
122
+ * `user@host`, as `ssh-keygen` does.
123
+ */
59
124
  public toOpenSSH(): string {
60
- return plugins.nodeForge.ssh.privateKeyToOpenSSH(this.forgePrivateKey, 'user@host');
125
+ return encodeOpenSshRsaPrivateKey(this.components, 'user@host');
126
+ }
127
+
128
+ private async getSigningKey(): Promise<CryptoKey> {
129
+ this.signingKey ??= this.importSigningKey();
130
+ return await this.signingKey;
131
+ }
132
+
133
+ private async importSigningKey(): Promise<CryptoKey> {
134
+ const privateKeyInfo = encodePrivateKeyInfo(this.components);
135
+ const privateKeyInfoBuffer = toArrayBuffer(privateKeyInfo);
136
+ try {
137
+ return await getWebCrypto().subtle.importKey(
138
+ 'pkcs8',
139
+ privateKeyInfoBuffer,
140
+ { name: 'RSASSA-PKCS1-v1_5', hash: 'SHA-256' },
141
+ false,
142
+ ['sign'],
143
+ );
144
+ } catch (error) {
145
+ if (error instanceof SmartCryptoError) {
146
+ throw error;
147
+ }
148
+ throw createSmartCryptoError('INVALID_KEY');
149
+ } finally {
150
+ privateKeyInfo.fill(0);
151
+ new Uint8Array(privateKeyInfoBuffer).fill(0);
152
+ }
61
153
  }
62
154
  }
@@ -1,17 +1,41 @@
1
- import * as plugins from './smartcrypto.plugins.js';
1
+ import { bytesEqual, getWebCrypto, toArrayBuffer } from './smartcrypto.internal.js';
2
+ import { encodeOpenSshRsaPublicKey } from './smartcrypto.openssh.js';
3
+ import { decodeBase64, decodeFirstPemBlock, encodePem } from './smartcrypto.pem.js';
4
+ import {
5
+ bitLengthOf,
6
+ copyRsaPublicKeyComponents,
7
+ decodeRsaPublicKey,
8
+ decodeSubjectPublicKeyInfo,
9
+ encodeRsaPublicKey,
10
+ encodeSubjectPublicKeyInfo,
11
+ type IRsaPublicKeyComponents,
12
+ } from './smartcrypto.rsa.js';
13
+
14
+ const spkiPemType = 'PUBLIC KEY';
15
+ const pkcs1PublicPemType = 'RSA PUBLIC KEY';
16
+ const canonicalPublicKeyErrorPrefix = 'Invalid canonical RSA public key:';
2
17
 
3
18
  const canonicalPublicKeyError = (messageArg: string): Error =>
4
- new Error(`Invalid canonical RSA public key: ${messageArg}`);
19
+ new Error(`${canonicalPublicKeyErrorPrefix} ${messageArg}`);
5
20
 
6
21
  export class PublicKey {
7
22
  // STATIC
23
+ /** Parses an SPKI or PKCS#1 RSA public-key PEM; despite its name it reads a public key. */
8
24
  public static createFromPrivateKey(pemString: string) {
9
- const privateKey = plugins.nodeForge.pki.publicKeyFromPem(pemString);
10
- return new PublicKey(privateKey);
25
+ return PublicKey.fromPemString(pemString);
11
26
  }
12
27
 
28
+ /**
29
+ * Parses the first PEM block of `pemString` as an SPKI (`PUBLIC KEY`) or
30
+ * PKCS#1 (`RSA PUBLIC KEY`) RSA public key. Fails with `INVALID_KEY`.
31
+ */
13
32
  public static fromPemString(pemString: string) {
14
- return new PublicKey(plugins.nodeForge.pki.publicKeyFromPem(pemString));
33
+ const pemBlock = decodeFirstPemBlock(pemString, [spkiPemType, pkcs1PublicPemType]);
34
+ return new PublicKey(
35
+ pemBlock.type === spkiPemType
36
+ ? decodeSubjectPublicKeyInfo(pemBlock.body)
37
+ : decodeRsaPublicKey(pemBlock.body),
38
+ );
15
39
  }
16
40
 
17
41
  /**
@@ -45,30 +69,27 @@ export class PublicKey {
45
69
  throw canonicalPublicKeyError('PEM body is malformed');
46
70
  }
47
71
 
72
+ let der: Uint8Array;
48
73
  try {
49
- const pemMessages = plugins.nodeForge.pem.decode(normalizedPem);
50
- if (pemMessages.length !== 1) {
51
- throw canonicalPublicKeyError('expected exactly one decoded PEM block');
52
- }
53
- const pemMessage = pemMessages[0];
54
- if (plugins.nodeForge.util.encode64(pemMessage.body) !== flattenedBase64) {
55
- throw canonicalPublicKeyError('PEM body is not canonical base64');
56
- }
57
- const publicKeyAsn1 = plugins.nodeForge.asn1.fromDer(pemMessage.body, true);
58
- const publicKey = plugins.nodeForge.pki.publicKeyFromAsn1(publicKeyAsn1);
59
- if (publicKey.n.bitLength() < 2_048) {
74
+ der = decodeBase64(flattenedBase64);
75
+ } catch {
76
+ throw canonicalPublicKeyError('PEM body is not canonical base64');
77
+ }
78
+
79
+ try {
80
+ const components = spkiMatch ? decodeSubjectPublicKeyInfo(der) : decodeRsaPublicKey(der);
81
+ if (bitLengthOf(components.modulus) < 2_048) {
60
82
  throw canonicalPublicKeyError('RSA modulus must contain at least 2048 bits');
61
83
  }
62
- const canonicalAsn1 = spkiMatch
63
- ? plugins.nodeForge.pki.publicKeyToAsn1(publicKey)
64
- : plugins.nodeForge.pki.publicKeyToRSAPublicKey(publicKey);
65
- const canonicalDer = plugins.nodeForge.asn1.toDer(canonicalAsn1).getBytes();
66
- if (canonicalDer !== pemMessage.body) {
84
+ const canonicalDer = spkiMatch
85
+ ? encodeSubjectPublicKeyInfo(components)
86
+ : encodeRsaPublicKey(components);
87
+ if (!bytesEqual(canonicalDer, der)) {
67
88
  throw canonicalPublicKeyError('public-key DER is not canonical');
68
89
  }
69
- return new PublicKey(publicKey);
90
+ return new PublicKey(components);
70
91
  } catch (error) {
71
- if (error instanceof Error && error.message.startsWith('Invalid canonical RSA public key:')) {
92
+ if (error instanceof Error && error.message.startsWith(canonicalPublicKeyErrorPrefix)) {
72
93
  throw error;
73
94
  }
74
95
  throw canonicalPublicKeyError('PEM does not encode a valid RSA public key');
@@ -76,79 +97,63 @@ export class PublicKey {
76
97
  }
77
98
 
78
99
  // INSTANCE
79
- public forgePublicKey: plugins.nodeForge.pki.rsa.PublicKey;
100
+ private readonly components: IRsaPublicKeyComponents;
101
+ private verificationKey?: Promise<CryptoKey>;
80
102
 
81
- constructor(publicKeyArg: plugins.nodeForge.pki.rsa.PublicKey) {
82
- this.forgePublicKey = publicKeyArg;
103
+ /** Creates a public key from its modulus and public exponent. Fails with `INVALID_KEY`. */
104
+ constructor(componentsArg: IRsaPublicKeyComponents) {
105
+ this.components = copyRsaPublicKeyComponents(componentsArg);
83
106
  }
84
107
 
108
+ /** The RSA modulus length in bits. */
109
+ public get modulusLength(): number {
110
+ return bitLengthOf(this.components.modulus);
111
+ }
112
+
113
+ /** Writes an SPKI `-----BEGIN PUBLIC KEY-----` PEM (64-column lines, CRLF). */
85
114
  public toPemString(): string {
86
- return plugins.nodeForge.pki.publicKeyToPem(this.forgePublicKey);
115
+ return encodePem(spkiPemType, encodeSubjectPublicKeyInfo(this.components));
87
116
  }
88
117
 
89
118
  /**
90
- * Verifies RSASSA-PKCS1-v1_5 with an exact SHA-256 DigestInfo. The exact
91
- * encoding check prevents accepting signatures that label the digest with a
92
- * different algorithm identifier.
119
+ * Verifies an RSASSA-PKCS1-v1_5 signature over SHA-256 with WebCrypto. The
120
+ * platform verifier compares the complete encoded message, so a signature
121
+ * whose DigestInfo names another algorithm or carries extra elements fails.
122
+ * Malformed input and invalid signatures resolve to `false`.
93
123
  */
94
- public verifyRs256(dataArg: Uint8Array, signatureArg: Uint8Array): boolean {
124
+ public async verifyRs256Signature(dataArg: Uint8Array, signatureArg: Uint8Array): Promise<boolean> {
95
125
  if (!(dataArg instanceof Uint8Array) || !(signatureArg instanceof Uint8Array)) {
96
126
  return false;
97
127
  }
128
+ if (signatureArg.byteLength !== Math.ceil(this.modulusLength / 8)) {
129
+ return false;
130
+ }
131
+ const subtle = getWebCrypto().subtle;
98
132
  try {
99
- if (signatureArg.byteLength !== Math.ceil(this.forgePublicKey.n.bitLength() / 8)) {
100
- return false;
101
- }
102
- const messageDigest = plugins.nodeForge.md.sha256.create();
103
- messageDigest.update(
104
- plugins.nodeForge.util.createBuffer(dataArg.slice()).getBytes(),
105
- 'raw',
106
- );
107
- const digestInfo = plugins.nodeForge.asn1.create(
108
- plugins.nodeForge.asn1.Class.UNIVERSAL,
109
- plugins.nodeForge.asn1.Type.SEQUENCE,
110
- true,
111
- [
112
- plugins.nodeForge.asn1.create(
113
- plugins.nodeForge.asn1.Class.UNIVERSAL,
114
- plugins.nodeForge.asn1.Type.SEQUENCE,
115
- true,
116
- [
117
- plugins.nodeForge.asn1.create(
118
- plugins.nodeForge.asn1.Class.UNIVERSAL,
119
- plugins.nodeForge.asn1.Type.OID,
120
- false,
121
- plugins.nodeForge.asn1.oidToDer(
122
- plugins.nodeForge.pki.oids.sha256,
123
- ).getBytes(),
124
- ),
125
- plugins.nodeForge.asn1.create(
126
- plugins.nodeForge.asn1.Class.UNIVERSAL,
127
- plugins.nodeForge.asn1.Type.NULL,
128
- false,
129
- '',
130
- ),
131
- ],
132
- ),
133
- plugins.nodeForge.asn1.create(
134
- plugins.nodeForge.asn1.Class.UNIVERSAL,
135
- plugins.nodeForge.asn1.Type.OCTETSTRING,
136
- false,
137
- messageDigest.digest().getBytes(),
138
- ),
139
- ],
140
- );
141
- return this.forgePublicKey.verify(
142
- plugins.nodeForge.asn1.toDer(digestInfo).getBytes(),
143
- plugins.nodeForge.util.createBuffer(signatureArg.slice()).getBytes(),
144
- 'NONE',
133
+ return await subtle.verify(
134
+ { name: 'RSASSA-PKCS1-v1_5' },
135
+ await this.getVerificationKey(),
136
+ toArrayBuffer(signatureArg),
137
+ toArrayBuffer(dataArg),
145
138
  );
146
139
  } catch {
147
140
  return false;
148
141
  }
149
142
  }
150
143
 
144
+ /** Writes an OpenSSH `ssh-rsa` public key line with the comment `user@host`. */
151
145
  public toOpenSSH(): string {
152
- return plugins.nodeForge.ssh.publicKeyToOpenSSH(this.forgePublicKey, 'user@host');
146
+ return encodeOpenSshRsaPublicKey(this.components, 'user@host');
147
+ }
148
+
149
+ private getVerificationKey(): Promise<CryptoKey> {
150
+ this.verificationKey ??= getWebCrypto().subtle.importKey(
151
+ 'spki',
152
+ toArrayBuffer(encodeSubjectPublicKeyInfo(this.components)),
153
+ { name: 'RSASSA-PKCS1-v1_5', hash: 'SHA-256' },
154
+ false,
155
+ ['verify'],
156
+ );
157
+ return this.verificationKey;
153
158
  }
154
159
  }
@@ -1,4 +1,3 @@
1
- import * as plugins from './smartcrypto.plugins.js';
2
1
  import { KeyPair, type IKeyPairOptions } from './smartcrypto.classes.keypair.js';
3
2
 
4
3
  export class Smartcrypto {