volter 0.0.171 → 0.0.173
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.js +141 -0
- package/dist/utils.js +14560 -0
- package/package.json +8 -5
- package/types/crypto.d.ts +21 -0
- package/types/types.d.ts +37 -0
- package/types/utils.d.ts +12 -0
- package/src/index.ts +0 -182
- package/src/receive.ts +0 -33
- package/src/session.ts +0 -37
- package/src/utils.ts +0 -148
- package/tsconfig.json +0 -23
package/dist/crypto.js
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __toESM = (mod, isNodeMode, target) => {
|
|
7
|
+
target = mod != null ? __create(__getProtoOf(mod)) : {};
|
|
8
|
+
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
9
|
+
for (let key of __getOwnPropNames(mod))
|
|
10
|
+
if (!__hasOwnProp.call(to, key))
|
|
11
|
+
__defProp(to, key, {
|
|
12
|
+
get: () => mod[key],
|
|
13
|
+
enumerable: true
|
|
14
|
+
});
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
18
|
+
var __export = (target, all) => {
|
|
19
|
+
for (var name in all)
|
|
20
|
+
__defProp(target, name, {
|
|
21
|
+
get: all[name],
|
|
22
|
+
enumerable: true,
|
|
23
|
+
configurable: true,
|
|
24
|
+
set: (newValue) => all[name] = () => newValue
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
// src/crypto.ts
|
|
29
|
+
function hash(password, hmac) {
|
|
30
|
+
const hasher = new Bun.CryptoHasher("sha256", hmac);
|
|
31
|
+
return hasher.update(password).digest("hex");
|
|
32
|
+
}
|
|
33
|
+
async function cryptoKey(raw) {
|
|
34
|
+
if (raw) {
|
|
35
|
+
if (raw instanceof CryptoKey)
|
|
36
|
+
return raw;
|
|
37
|
+
if (raw instanceof Uint8Array) {
|
|
38
|
+
if (raw.length === 32) {
|
|
39
|
+
return await crypto.subtle.importKey("raw", raw, { name: "AES-GCM", length: 256 }, true, ["encrypt", "decrypt"]);
|
|
40
|
+
}
|
|
41
|
+
return await crypto.subtle.importKey("raw", raw, { name: "AES-GCM", length: 256 }, true, ["encrypt", "decrypt"]);
|
|
42
|
+
}
|
|
43
|
+
if (raw.length === 64) {
|
|
44
|
+
return await crypto.subtle.importKey("raw", Uint8Array.fromHex(raw), {
|
|
45
|
+
name: "AES-GCM",
|
|
46
|
+
length: 256
|
|
47
|
+
}, true, ["encrypt", "decrypt"]);
|
|
48
|
+
} else {
|
|
49
|
+
return await crypto.subtle.importKey("raw", Uint8Array.fromHex(hash(raw)), {
|
|
50
|
+
name: "AES-GCM",
|
|
51
|
+
length: 256
|
|
52
|
+
}, true, ["encrypt", "decrypt"]);
|
|
53
|
+
}
|
|
54
|
+
} else {
|
|
55
|
+
return await crypto.subtle.generateKey({
|
|
56
|
+
name: "AES-GCM",
|
|
57
|
+
length: 256
|
|
58
|
+
}, true, ["encrypt", "decrypt"]);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
async function exportKey(key, format) {
|
|
62
|
+
const buffer = await crypto.subtle.exportKey(format, key);
|
|
63
|
+
return new Uint8Array(buffer).toBase64();
|
|
64
|
+
}
|
|
65
|
+
async function generateECDSAKeyPair() {
|
|
66
|
+
return await crypto.subtle.generateKey({
|
|
67
|
+
name: "ECDSA",
|
|
68
|
+
namedCurve: "P-256"
|
|
69
|
+
}, true, ["sign", "verify"]);
|
|
70
|
+
}
|
|
71
|
+
async function exportJWK(key) {
|
|
72
|
+
return await crypto.subtle.exportKey("jwk", key);
|
|
73
|
+
}
|
|
74
|
+
async function importJWK(jwk, usage) {
|
|
75
|
+
return await crypto.subtle.importKey("jwk", jwk, { name: "ECDSA", namedCurve: "P-256" }, true, usage);
|
|
76
|
+
}
|
|
77
|
+
async function sign(message, key) {
|
|
78
|
+
const data = new TextEncoder().encode(message);
|
|
79
|
+
const signature = await crypto.subtle.sign({
|
|
80
|
+
name: "ECDSA",
|
|
81
|
+
hash: { name: "SHA-256" }
|
|
82
|
+
}, key, data);
|
|
83
|
+
return new Uint8Array(signature);
|
|
84
|
+
}
|
|
85
|
+
async function verifySign(message, signature, key) {
|
|
86
|
+
const data = new TextEncoder().encode(message);
|
|
87
|
+
return await crypto.subtle.verify({
|
|
88
|
+
name: "ECDSA",
|
|
89
|
+
hash: { name: "SHA-256" }
|
|
90
|
+
}, key, signature, data);
|
|
91
|
+
}
|
|
92
|
+
async function cipher(text, key) {
|
|
93
|
+
const encoder = new TextEncoder;
|
|
94
|
+
const data = encoder.encode(text);
|
|
95
|
+
const iv = crypto.getRandomValues(new Uint8Array(12));
|
|
96
|
+
const buffer = await crypto.subtle.encrypt({ name: "AES-GCM", iv }, await cryptoKey(key), data);
|
|
97
|
+
return {
|
|
98
|
+
text: new Uint8Array(buffer).toHex(),
|
|
99
|
+
iv,
|
|
100
|
+
buffer
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
async function decipher(cipher2, key) {
|
|
104
|
+
const decoder = new TextDecoder;
|
|
105
|
+
const buffer = await crypto.subtle.decrypt({ name: "AES-GCM", iv: cipher2.iv }, await cryptoKey(key), Uint8Array.fromHex(cipher2.text));
|
|
106
|
+
const data = decoder.decode(buffer);
|
|
107
|
+
return data;
|
|
108
|
+
}
|
|
109
|
+
async function keypair() {
|
|
110
|
+
return await crypto.subtle.generateKey({
|
|
111
|
+
name: "RSA-OAEP",
|
|
112
|
+
modulusLength: 2048,
|
|
113
|
+
publicExponent: new Uint8Array([1, 0, 1]),
|
|
114
|
+
hash: { name: "SHA-256" }
|
|
115
|
+
}, true, ["encrypt", "decrypt"]);
|
|
116
|
+
}
|
|
117
|
+
async function encrypt(text, key) {
|
|
118
|
+
const data = new TextEncoder().encode(text);
|
|
119
|
+
const buffer = await crypto.subtle.encrypt({ name: "RSA-OAEP" }, key, data);
|
|
120
|
+
return new Uint8Array(buffer);
|
|
121
|
+
}
|
|
122
|
+
async function decrypt(text, key) {
|
|
123
|
+
const buffer = await crypto.subtle.decrypt({ name: "RSA-OAEP" }, key, text);
|
|
124
|
+
const data = new TextDecoder().decode(buffer);
|
|
125
|
+
return data;
|
|
126
|
+
}
|
|
127
|
+
export {
|
|
128
|
+
verifySign,
|
|
129
|
+
sign,
|
|
130
|
+
keypair,
|
|
131
|
+
importJWK,
|
|
132
|
+
hash,
|
|
133
|
+
generateECDSAKeyPair,
|
|
134
|
+
exportKey,
|
|
135
|
+
exportJWK,
|
|
136
|
+
encrypt,
|
|
137
|
+
decrypt,
|
|
138
|
+
decipher,
|
|
139
|
+
cryptoKey,
|
|
140
|
+
cipher
|
|
141
|
+
};
|