Skip to content

Commit d01fa59

Browse files
authored
Merge pull request #81 from PeculiarVentures/chore/update-peculiar-utils
chore: migrate to @peculiar/utils
2 parents fe4fa17 + 73f7999 commit d01fa59

15 files changed

Lines changed: 158 additions & 110 deletions

File tree

package-lock.json

Lines changed: 19 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@
6969
"dependencies": {
7070
"@peculiar/asn1-schema": "^2.6.0",
7171
"@peculiar/json-schema": "^1.1.12",
72-
"pvtsutils": "^1.3.6",
72+
"@peculiar/utils": "^2.0.1",
7373
"tslib": "^2.8.1",
74-
"webcrypto-core": "^1.8.1"
74+
"webcrypto-core": "^1.9.0"
7575
},
7676
"engines": {
7777
"node": ">=14.18.0"

src/converters/base64_url.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Buffer } from "node:buffer";
22
import { IJsonConverter } from "@peculiar/json-schema";
3-
import { Convert } from "pvtsutils";
3+
import { convert } from "@peculiar/utils";
44

55
export const JsonBase64UrlConverter: IJsonConverter<Buffer, string> = {
6-
fromJSON: (value: string) => Buffer.from(Convert.FromBase64Url(value)),
7-
toJSON: (value: Buffer) => Convert.ToBase64Url(value),
6+
fromJSON: (value: string) => Buffer.from(convert.decode("base64url", value)),
7+
toJSON: (value: Buffer) => convert.encode("base64url", value),
88
};

src/mechs/aes/crypto.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { Buffer } from "node:buffer";
22
import crypto, { CipherGCMTypes } from "node:crypto";
33
import { JsonParser, JsonSerializer } from "@peculiar/json-schema";
4+
import {
5+
assertBufferSource, toArrayBuffer, toUint8Array,
6+
} from "@peculiar/utils";
47
import * as core from "webcrypto-core";
58
import { CryptoKey } from "../../keys";
69
import { AesCryptoKey } from "./key";
@@ -27,7 +30,7 @@ export class AesCrypto {
2730
case "jwk":
2831
return JsonSerializer.toJSON(key);
2932
case "raw":
30-
return new Uint8Array(key.data).buffer;
33+
return toArrayBuffer(key.data);
3134
default:
3235
throw new core.OperationError("format: Must be 'jwk' or 'raw'");
3336
}
@@ -41,8 +44,9 @@ export class AesCrypto {
4144
key = JsonParser.fromJSON(keyData, { targetSchema: AesCryptoKey });
4245
break;
4346
case "raw":
47+
assertBufferSource(keyData);
4448
key = new AesCryptoKey();
45-
key.data = Buffer.from(keyData as ArrayBuffer);
49+
key.data = Buffer.from(toUint8Array(keyData));
4650
break;
4751
default:
4852
throw new core.OperationError("format: Must be 'jwk' or 'raw'");
@@ -105,91 +109,91 @@ export class AesCrypto {
105109
}
106110

107111
public static async encryptAesCBC(algorithm: AesCbcParams, key: AesCryptoKey, data: Buffer) {
108-
const cipher = crypto.createCipheriv(`aes-${key.algorithm.length}-cbc`, key.data, new Uint8Array(algorithm.iv as ArrayBuffer));
112+
const cipher = crypto.createCipheriv(`aes-${key.algorithm.length}-cbc`, key.data, toUint8Array(algorithm.iv));
109113
let enc = cipher.update(data);
110114
enc = Buffer.concat([enc, cipher.final()]);
111-
const res = new Uint8Array(enc).buffer;
115+
const res = toArrayBuffer(enc);
112116
return res;
113117
}
114118

115119
public static async decryptAesCBC(algorithm: AesCbcParams, key: AesCryptoKey, data: Buffer) {
116-
const decipher = crypto.createDecipheriv(`aes-${key.algorithm.length}-cbc`, key.data, new Uint8Array(algorithm.iv as ArrayBuffer));
120+
const decipher = crypto.createDecipheriv(`aes-${key.algorithm.length}-cbc`, key.data, toUint8Array(algorithm.iv));
117121
let dec = decipher.update(data);
118122
dec = Buffer.concat([dec, decipher.final()]);
119-
return new Uint8Array(dec).buffer;
123+
return toArrayBuffer(dec);
120124
}
121125

122126
public static async encryptAesCTR(algorithm: AesCtrParams, key: AesCryptoKey, data: Buffer) {
123-
const cipher = crypto.createCipheriv(`aes-${key.algorithm.length}-ctr`, key.data, Buffer.from(algorithm.counter as ArrayBuffer));
127+
const cipher = crypto.createCipheriv(`aes-${key.algorithm.length}-ctr`, key.data, Buffer.from(toUint8Array(algorithm.counter)));
124128
let enc = cipher.update(data);
125129
enc = Buffer.concat([enc, cipher.final()]);
126-
const res = new Uint8Array(enc).buffer;
130+
const res = toArrayBuffer(enc);
127131
return res;
128132
}
129133

130134
public static async decryptAesCTR(algorithm: AesCtrParams, key: AesCryptoKey, data: Buffer) {
131-
const decipher = crypto.createDecipheriv(`aes-${key.algorithm.length}-ctr`, key.data, new Uint8Array(algorithm.counter as ArrayBuffer));
135+
const decipher = crypto.createDecipheriv(`aes-${key.algorithm.length}-ctr`, key.data, toUint8Array(algorithm.counter));
132136
let dec = decipher.update(data);
133137
dec = Buffer.concat([dec, decipher.final()]);
134-
return new Uint8Array(dec).buffer;
138+
return toArrayBuffer(dec);
135139
}
136140

137141
public static async encryptAesGCM(algorithm: AesGcmParams, key: AesCryptoKey, data: Buffer) {
138142
const cipher = crypto.createCipheriv(
139143
`aes-${key.algorithm.length}-gcm` as CipherGCMTypes,
140144
key.data,
141-
Buffer.from(algorithm.iv as ArrayBuffer),
145+
Buffer.from(toUint8Array(algorithm.iv)),
142146
{ authTagLength: (algorithm.tagLength || 128) >> 3 },
143147
); // NodeJs d.ts doesn't support CipherGCMOptions for createCipheriv
144148
if (algorithm.additionalData) {
145-
cipher.setAAD(Buffer.from(algorithm.additionalData as ArrayBuffer));
149+
cipher.setAAD(Buffer.from(toUint8Array(algorithm.additionalData)));
146150
}
147151
let enc = cipher.update(data);
148152
enc = Buffer.concat([enc, cipher.final(), cipher.getAuthTag()]);
149-
const res = new Uint8Array(enc).buffer;
153+
const res = toArrayBuffer(enc);
150154
return res;
151155
}
152156

153157
public static async decryptAesGCM(algorithm: AesGcmParams, key: AesCryptoKey, data: Buffer) {
154158
const tagLength = (algorithm.tagLength || 128) >> 3;
155-
const decipher = crypto.createDecipheriv(`aes-${key.algorithm.length}-gcm` as CipherGCMTypes, key.data, new Uint8Array(algorithm.iv as ArrayBuffer), { authTagLength: tagLength });
159+
const decipher = crypto.createDecipheriv(`aes-${key.algorithm.length}-gcm` as CipherGCMTypes, key.data, toUint8Array(algorithm.iv), { authTagLength: tagLength });
156160
const enc = data.slice(0, data.length - tagLength);
157161
const tag = data.slice(data.length - tagLength);
158162
if (algorithm.additionalData) {
159-
decipher.setAAD(Buffer.from(algorithm.additionalData as ArrayBuffer));
163+
decipher.setAAD(Buffer.from(toUint8Array(algorithm.additionalData)));
160164
}
161165
decipher.setAuthTag(tag);
162166
let dec = decipher.update(enc);
163167
dec = Buffer.concat([dec, decipher.final()]);
164-
return new Uint8Array(dec).buffer;
168+
return toArrayBuffer(dec);
165169
}
166170

167171
public static async encryptAesKW(algorithm: Algorithm, key: AesCryptoKey, data: Buffer) {
168172
const cipher = crypto.createCipheriv(`id-aes${key.algorithm.length}-wrap`, key.data, this.AES_KW_IV);
169173
let enc = cipher.update(data);
170174
enc = Buffer.concat([enc, cipher.final()]);
171-
return new Uint8Array(enc).buffer;
175+
return toArrayBuffer(enc);
172176
}
173177

174178
public static async decryptAesKW(algorithm: Algorithm, key: AesCryptoKey, data: Buffer) {
175179
const decipher = crypto.createDecipheriv(`id-aes${key.algorithm.length}-wrap`, key.data, this.AES_KW_IV);
176180
let dec = decipher.update(data);
177181
dec = Buffer.concat([dec, decipher.final()]);
178-
return new Uint8Array(dec).buffer;
182+
return toArrayBuffer(dec);
179183
}
180184

181185
public static async encryptAesECB(algorithm: Algorithm, key: AesCryptoKey, data: Buffer) {
182186
const cipher = crypto.createCipheriv(`aes-${key.algorithm.length}-ecb`, key.data, new Uint8Array(0));
183187
let enc = cipher.update(data);
184188
enc = Buffer.concat([enc, cipher.final()]);
185-
const res = new Uint8Array(enc).buffer;
189+
const res = toArrayBuffer(enc);
186190
return res;
187191
}
188192

189193
public static async decryptAesECB(algorithm: Algorithm, key: AesCryptoKey, data: Buffer) {
190194
const decipher = crypto.createDecipheriv(`aes-${key.algorithm.length}-ecb`, key.data, new Uint8Array(0));
191195
let dec = decipher.update(data);
192196
dec = Buffer.concat([dec, decipher.final()]);
193-
return new Uint8Array(dec).buffer;
197+
return toArrayBuffer(dec);
194198
}
195199
}

src/mechs/des/crypto.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { Buffer } from "node:buffer";
22
import crypto from "node:crypto";
33
import { JsonParser, JsonSerializer } from "@peculiar/json-schema";
4+
import {
5+
assertBufferSource, toArrayBuffer, toUint8Array,
6+
} from "@peculiar/utils";
47
import * as core from "webcrypto-core";
58
import { DesParams } from "webcrypto-core";
69
import { CryptoKey } from "../../keys";
@@ -22,7 +25,7 @@ export class DesCrypto {
2225
case "jwk":
2326
return JsonSerializer.toJSON(key);
2427
case "raw":
25-
return new Uint8Array(key.data).buffer;
28+
return toArrayBuffer(key.data);
2629
default:
2730
throw new core.OperationError("format: Must be 'jwk' or 'raw'");
2831
}
@@ -36,8 +39,9 @@ export class DesCrypto {
3639
key = JsonParser.fromJSON(keyData, { targetSchema: DesCryptoKey });
3740
break;
3841
case "raw":
42+
assertBufferSource(keyData);
3943
key = new DesCryptoKey();
40-
key.data = Buffer.from(keyData as ArrayBuffer);
44+
key.data = Buffer.from(toUint8Array(keyData));
4145
break;
4246
default:
4347
throw new core.OperationError("format: Must be 'jwk' or 'raw'");
@@ -77,32 +81,32 @@ export class DesCrypto {
7781
}
7882

7983
public static async encryptDesCBC(algorithm: DesParams, key: DesCryptoKey, data: Buffer) {
80-
const cipher = crypto.createCipheriv("des-cbc", key.data, new Uint8Array(algorithm.iv as ArrayBuffer));
84+
const cipher = crypto.createCipheriv("des-cbc", key.data, toUint8Array(algorithm.iv));
8185
let enc = cipher.update(data);
8286
enc = Buffer.concat([enc, cipher.final()]);
83-
const res = new Uint8Array(enc).buffer;
87+
const res = toArrayBuffer(enc);
8488
return res;
8589
}
8690

8791
public static async decryptDesCBC(algorithm: DesParams, key: DesCryptoKey, data: Buffer) {
88-
const decipher = crypto.createDecipheriv("des-cbc", key.data, new Uint8Array(algorithm.iv as ArrayBuffer));
92+
const decipher = crypto.createDecipheriv("des-cbc", key.data, toUint8Array(algorithm.iv));
8993
let dec = decipher.update(data);
9094
dec = Buffer.concat([dec, decipher.final()]);
91-
return new Uint8Array(dec).buffer;
95+
return toArrayBuffer(dec);
9296
}
9397

9498
public static async encryptDesEDE3CBC(algorithm: DesParams, key: DesCryptoKey, data: Buffer) {
95-
const cipher = crypto.createCipheriv("des-ede3-cbc", key.data, Buffer.from(algorithm.iv as ArrayBuffer));
99+
const cipher = crypto.createCipheriv("des-ede3-cbc", key.data, Buffer.from(toUint8Array(algorithm.iv)));
96100
let enc = cipher.update(data);
97101
enc = Buffer.concat([enc, cipher.final()]);
98-
const res = new Uint8Array(enc).buffer;
102+
const res = toArrayBuffer(enc);
99103
return res;
100104
}
101105

102106
public static async decryptDesEDE3CBC(algorithm: DesParams, key: DesCryptoKey, data: Buffer) {
103-
const decipher = crypto.createDecipheriv("des-ede3-cbc", key.data, new Uint8Array(algorithm.iv as ArrayBuffer));
107+
const decipher = crypto.createDecipheriv("des-ede3-cbc", key.data, toUint8Array(algorithm.iv));
104108
let dec = decipher.update(data);
105109
dec = Buffer.concat([dec, decipher.final()]);
106-
return new Uint8Array(dec).buffer;
110+
return toArrayBuffer(dec);
107111
}
108112
}

src/mechs/ec/crypto.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { Buffer } from "node:buffer";
22
import crypto from "node:crypto";
33
import { AsnParser, AsnSerializer } from "@peculiar/asn1-schema";
44
import { JsonParser, JsonSerializer } from "@peculiar/json-schema";
5-
import { BufferSourceConverter } from "pvtsutils";
5+
import {
6+
assertBufferSource, toArrayBuffer, toUint8Array,
7+
} from "@peculiar/utils";
68
import * as core from "webcrypto-core";
79
import { CryptoKey } from "../../keys";
810
import { ShaCrypto } from "../sha";
@@ -63,7 +65,7 @@ export class EcCrypto {
6365

6466
const signatureRaw = core.EcUtils.encodeSignature(ecSignature, core.EcCurves.get(key.algorithm.namedCurve).size);
6567

66-
return BufferSourceConverter.toArrayBuffer(signatureRaw);
68+
return toArrayBuffer(signatureRaw);
6769
}
6870

6971
public static async verify(algorithm: EcdsaParams, key: EcPublicKey, signature: Uint8Array, data: Uint8Array): Promise<boolean> {
@@ -79,8 +81,8 @@ export class EcCrypto {
7981
const ecSignature = new core.asn1.EcDsaSignature();
8082
const namedCurve = core.EcCurves.get(key.algorithm.namedCurve);
8183
const signaturePoint = core.EcUtils.decodeSignature(signature, namedCurve.size);
82-
ecSignature.r = BufferSourceConverter.toArrayBuffer(signaturePoint.r);
83-
ecSignature.s = BufferSourceConverter.toArrayBuffer(signaturePoint.s);
84+
ecSignature.r = toArrayBuffer(signaturePoint.r);
85+
ecSignature.s = toArrayBuffer(signaturePoint.s);
8486

8587
const ecSignatureRaw = Buffer.from(AsnSerializer.serialize(ecSignature));
8688
const ok = signer.verify(options, ecSignatureRaw);
@@ -99,10 +101,10 @@ export class EcCrypto {
99101
const bits = ecdh.computeSecret(Buffer.from(asnPublicKey.publicKey));
100102

101103
if (length === null) {
102-
return BufferSourceConverter.toArrayBuffer(bits);
104+
return toArrayBuffer(bits);
103105
}
104106

105-
return new Uint8Array(bits).buffer.slice(0, length >> 3);
107+
return toArrayBuffer(bits).slice(0, length >> 3);
106108
}
107109

108110
public static async exportKey(format: KeyFormat, key: CryptoKey): Promise<JsonWebKey | ArrayBuffer> {
@@ -111,7 +113,7 @@ export class EcCrypto {
111113
return JsonSerializer.toJSON(key);
112114
case "pkcs8":
113115
case "spki":
114-
return new Uint8Array(key.data).buffer;
116+
return toArrayBuffer(key.data);
115117
case "raw": {
116118
const publicKeyInfo = AsnParser.parse(key.data, core.asn1.PublicKeyInfo);
117119
return publicKeyInfo.publicKey;
@@ -134,17 +136,20 @@ export class EcCrypto {
134136
}
135137
}
136138
case "raw": {
137-
const asnKey = new core.asn1.EcPublicKey(keyData as ArrayBuffer);
139+
assertBufferSource(keyData);
140+
const asnKey = new core.asn1.EcPublicKey(toArrayBuffer(keyData));
138141
return this.importPublicKey(asnKey, algorithm, extractable, keyUsages);
139142
}
140143
case "spki": {
141-
const keyInfo = AsnParser.parse(new Uint8Array(keyData as ArrayBuffer), core.asn1.PublicKeyInfo);
144+
assertBufferSource(keyData);
145+
const keyInfo = AsnParser.parse(toUint8Array(keyData), core.asn1.PublicKeyInfo);
142146
const asnKey = new core.asn1.EcPublicKey(keyInfo.publicKey);
143147
this.assertKeyParameters(keyInfo.publicKeyAlgorithm.parameters, algorithm.namedCurve);
144148
return this.importPublicKey(asnKey, algorithm, extractable, keyUsages);
145149
}
146150
case "pkcs8": {
147-
const keyInfo = AsnParser.parse(new Uint8Array(keyData as ArrayBuffer), core.asn1.PrivateKeyInfo);
151+
assertBufferSource(keyData);
152+
const keyInfo = AsnParser.parse(toUint8Array(keyData), core.asn1.PrivateKeyInfo);
148153
const asnKey = AsnParser.parse(keyInfo.privateKey, core.asn1.EcPrivateKey);
149154
this.assertKeyParameters(keyInfo.privateKeyAlgorithm.parameters, algorithm.namedCurve);
150155
return this.importPrivateKey(asnKey, algorithm, extractable, keyUsages);

0 commit comments

Comments
 (0)