@aamp/protocol 1.1.1 → 1.1.2
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.
- package/dist/crypto.d.ts +1 -0
- package/dist/crypto.js +9 -0
- package/package.json +1 -1
- package/src/crypto.ts +16 -0
package/dist/crypto.d.ts
CHANGED
|
@@ -6,3 +6,4 @@ export declare function generateKeyPair(): Promise<CryptoKeyPair>;
|
|
|
6
6
|
export declare function signData(privateKey: CryptoKey, data: string): Promise<string>;
|
|
7
7
|
export declare function verifySignature(publicKey: CryptoKey, data: string, signatureHex: string): Promise<boolean>;
|
|
8
8
|
export declare function exportPublicKey(key: CryptoKey): Promise<string>;
|
|
9
|
+
export declare function importPublicKey(keyData: string): Promise<CryptoKey>;
|
package/dist/crypto.js
CHANGED
|
@@ -8,6 +8,7 @@ exports.generateKeyPair = generateKeyPair;
|
|
|
8
8
|
exports.signData = signData;
|
|
9
9
|
exports.verifySignature = verifySignature;
|
|
10
10
|
exports.exportPublicKey = exportPublicKey;
|
|
11
|
+
exports.importPublicKey = importPublicKey;
|
|
11
12
|
async function generateKeyPair() {
|
|
12
13
|
// Uses standard Web Crypto API (Node 19+ compatible)
|
|
13
14
|
return await crypto.subtle.generateKey({ name: "ECDSA", namedCurve: "P-256" }, true, ["sign", "verify"]);
|
|
@@ -28,6 +29,14 @@ async function exportPublicKey(key) {
|
|
|
28
29
|
const exported = await crypto.subtle.exportKey("spki", key);
|
|
29
30
|
return btoa(String.fromCharCode(...new Uint8Array(exported)));
|
|
30
31
|
}
|
|
32
|
+
async function importPublicKey(keyData) {
|
|
33
|
+
const binaryString = atob(keyData);
|
|
34
|
+
const bytes = new Uint8Array(binaryString.length);
|
|
35
|
+
for (let i = 0; i < binaryString.length; i++) {
|
|
36
|
+
bytes[i] = binaryString.charCodeAt(i);
|
|
37
|
+
}
|
|
38
|
+
return await crypto.subtle.importKey("spki", bytes, { name: "ECDSA", namedCurve: "P-256" }, true, ["verify"]);
|
|
39
|
+
}
|
|
31
40
|
// Helpers
|
|
32
41
|
function bufToHex(buffer) {
|
|
33
42
|
return Array.from(new Uint8Array(buffer))
|
package/package.json
CHANGED
package/src/crypto.ts
CHANGED
|
@@ -41,6 +41,22 @@ export async function exportPublicKey(key: CryptoKey): Promise<string> {
|
|
|
41
41
|
return btoa(String.fromCharCode(...new Uint8Array(exported)));
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
+
export async function importPublicKey(keyData: string): Promise<CryptoKey> {
|
|
45
|
+
const binaryString = atob(keyData);
|
|
46
|
+
const bytes = new Uint8Array(binaryString.length);
|
|
47
|
+
for (let i = 0; i < binaryString.length; i++) {
|
|
48
|
+
bytes[i] = binaryString.charCodeAt(i);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return await crypto.subtle.importKey(
|
|
52
|
+
"spki",
|
|
53
|
+
bytes,
|
|
54
|
+
{ name: "ECDSA", namedCurve: "P-256" },
|
|
55
|
+
true,
|
|
56
|
+
["verify"]
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
44
60
|
// Helpers
|
|
45
61
|
function bufToHex(buffer: ArrayBuffer): string {
|
|
46
62
|
return Array.from(new Uint8Array(buffer))
|