@peculiar/webcrypto
Advanced tools
Comparing version
47
index.js
@@ -703,3 +703,3 @@ // Copyright (c) 2019, Peculiar Ventures, All rights reserved. | ||
const bytes = new Uint8Array(value); | ||
if (bytes[0] > 128) { | ||
if (bytes[0] > 127) { | ||
const newValue = new Uint8Array(bytes.length + 1); | ||
@@ -1169,6 +1169,7 @@ newValue.set(bytes, 1); | ||
const ecSignature = asn1Schema.AsnParser.parse(signature, EcDsaSignature); | ||
return new Uint8Array(Buffer.concat([ | ||
Buffer.from(ecSignature.r), | ||
Buffer.from(ecSignature.s), | ||
])).buffer; | ||
const pointSize = this.getPointSize(key.algorithm.namedCurve); | ||
const r = this.addPadding(pointSize, Buffer.from(ecSignature.r)); | ||
const s = this.addPadding(pointSize, Buffer.from(ecSignature.s)); | ||
const signatureRaw = new Uint8Array(Buffer.concat([r, s])).buffer; | ||
return signatureRaw; | ||
} | ||
@@ -1183,6 +1184,7 @@ static async verify(algorithm, key, signature, data) { | ||
const ecSignature = new EcDsaSignature(); | ||
const size = signature.length / 2; | ||
ecSignature.r = signature.buffer.slice(0, size); | ||
ecSignature.s = signature.buffer.slice(size, size + size); | ||
const ok = signer.verify(options, Buffer.from(asn1Schema.AsnSerializer.serialize(ecSignature))); | ||
const pointSize = this.getPointSize(key.algorithm.namedCurve); | ||
ecSignature.r = this.removePadding(signature.slice(0, pointSize)); | ||
ecSignature.s = this.removePadding(signature.slice(pointSize, pointSize + pointSize)); | ||
const ecSignatureRaw = Buffer.from(asn1Schema.AsnSerializer.serialize(ecSignature)); | ||
const ok = signer.verify(options, ecSignatureRaw); | ||
return ok; | ||
@@ -1282,2 +1284,29 @@ } | ||
} | ||
static getPointSize(namedCurve) { | ||
switch (namedCurve) { | ||
case "P-256": | ||
case "K-256": | ||
return 32; | ||
case "P-384": | ||
return 48; | ||
case "P-521": | ||
return 66; | ||
default: | ||
throw new Error(`Cannot get size for the named curve '${namedCurve}'`); | ||
} | ||
} | ||
static addPadding(pointSize, bytes) { | ||
const res = Buffer.alloc(pointSize); | ||
res.set(Buffer.from(bytes), pointSize - bytes.length); | ||
return res; | ||
} | ||
static removePadding(bytes) { | ||
for (let i = 0; i < bytes.length; i++) { | ||
if (!bytes[i]) { | ||
continue; | ||
} | ||
return bytes.slice(i).buffer; | ||
} | ||
return new ArrayBuffer(0); | ||
} | ||
} | ||
@@ -1284,0 +1313,0 @@ EcCrypto.publicKeyUsages = ["verify"]; |
{ | ||
"name": "@peculiar/webcrypto", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "A WebCrypto Polyfill for NodeJS", | ||
@@ -5,0 +5,0 @@ "repository": { |
70296
1.59%1540
1.92%