Skip to content

Commit 0a6ed80

Browse files
committed
fix: cursor comment
1 parent c6f6dce commit 0a6ed80

File tree

1 file changed

+8
-19
lines changed

1 file changed

+8
-19
lines changed

src/index.ts

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { secp256k1 } from "@noble/curves/secp256k1.js";
2-
import { concatBytes } from "@noble/curves/utils.js";
2+
import { concatBytes, equalBytes } from "@noble/curves/utils.js";
33

44
// eslint-disable-next-line @typescript-eslint/no-explicit-any, n/no-unsupported-features/node-builtins
55
const browserCrypto = globalThis.crypto || (globalThis as any).msCrypto || {};
@@ -21,7 +21,7 @@ function assert(condition: boolean, message: string) {
2121
}
2222
}
2323

24-
export function uint8ArrayToBigInt(arr: Uint8Array): bigint {
24+
function uint8ArrayToBigInt(arr: Uint8Array): bigint {
2525
let result = 0n;
2626
for (let i = 0; i < arr.length; i++) {
2727
result = (result << 8n) | BigInt(arr[i]);
@@ -41,19 +41,6 @@ function isValidPrivateKey(privateKey: Uint8Array): boolean {
4141
); // < G
4242
}
4343

44-
// Compare two buffers in constant time to prevent timing attacks.
45-
function equalConstTime(b1: Uint8Array, b2: Uint8Array): boolean {
46-
if (b1.length !== b2.length) {
47-
return false;
48-
}
49-
let res = 0;
50-
for (let i = 0; i < b1.length; i++) {
51-
res |= b1[i] ^ b2[i];
52-
}
53-
54-
return res === 0;
55-
}
56-
5744
/* This must check if we're in the browser or
5845
not, since the functions are different and does
5946
not convert using browserify */
@@ -133,7 +120,7 @@ async function hmacSha256Sign(key: Uint8Array, msg: Uint8Array): Promise<Uint8Ar
133120

134121
async function hmacSha256Verify(key: Uint8Array, msg: Uint8Array, sig: Uint8Array): Promise<boolean> {
135122
const expectedSig = await hmacSha256Sign(key, msg);
136-
return equalConstTime(expectedSig, sig);
123+
return equalBytes(expectedSig, sig);
137124
}
138125

139126
function assertValidPrivateKey(privateKey: Uint8Array): void {
@@ -184,7 +171,10 @@ export const getPublicCompressed = function (privateKey: Uint8Array): Uint8Array
184171
export const sign = async function (privateKey: Uint8Array, msg: Uint8Array): Promise<Uint8Array> {
185172
assertValidPrivateKey(privateKey);
186173
assertValidMessage(msg);
187-
const sig = secp256k1.sign(msg, privateKey, { prehash: false, format: "der" });
174+
const sig = secp256k1.sign(msg, privateKey, {
175+
prehash: false,
176+
format: "der",
177+
});
188178
return sig;
189179
};
190180

@@ -237,8 +227,7 @@ export const encrypt = async function (
237227
const iv = opts.iv || randomBytes(16);
238228
const encryptionKey = hash.slice(0, 32);
239229
const macKey = hash.slice(32);
240-
const data = await aesCbcEncrypt(iv, encryptionKey, msg);
241-
const ciphertext = data;
230+
const ciphertext = await aesCbcEncrypt(iv, encryptionKey, msg);
242231
const dataToMac = concatBytes(iv, ephemPublicKey, ciphertext);
243232
const mac = await hmacSha256Sign(macKey, dataToMac);
244233
return {

0 commit comments

Comments
 (0)