@ka-libs/crypto 1.0.0
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/LICENSE +21 -0
- package/README.md +246 -0
- package/dist/cjs/index.cjs +1 -0
- package/dist/esm/core/getCrypto.js +1 -0
- package/dist/esm/core/getSubtle.js +1 -0
- package/dist/esm/core/importPrivateKey.js +1 -0
- package/dist/esm/core/importPublicKey.js +1 -0
- package/dist/esm/core/keyPairs.js +1 -0
- package/dist/esm/cripter/aesDecrypt.js +1 -0
- package/dist/esm/cripter/aesEncrypt.js +1 -0
- package/dist/esm/cripter/decrypt.js +1 -0
- package/dist/esm/cripter/encrypt.js +1 -0
- package/dist/esm/cripter/rsaDecrypt.js +1 -0
- package/dist/esm/cripter/rsaEncrypt.js +1 -0
- package/dist/esm/exports/aes-decrypt.js +1 -0
- package/dist/esm/exports/aes-encrypt.js +1 -0
- package/dist/esm/exports/array-buffer-to-base64.js +1 -0
- package/dist/esm/exports/base64-cleaner.js +1 -0
- package/dist/esm/exports/base64-to-array-buffer.js +1 -0
- package/dist/esm/exports/decrypt.js +1 -0
- package/dist/esm/exports/encrypt.js +1 -0
- package/dist/esm/exports/export-key-pairs.js +1 -0
- package/dist/esm/exports/get-random-bytes.js +1 -0
- package/dist/esm/exports/get-random-values.js +1 -0
- package/dist/esm/exports/key-pairs.js +1 -0
- package/dist/esm/exports/rsa-decrypt.js +1 -0
- package/dist/esm/exports/rsa-encrypt.js +1 -0
- package/dist/esm/exports/uuidv4.js +1 -0
- package/dist/esm/exports/uuidv5.js +1 -0
- package/dist/esm/exports.js +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/lib/arrayBufferToBase64.js +1 -0
- package/dist/esm/lib/base64Cleaner.js +1 -0
- package/dist/esm/lib/base64ToArrayBuffer.js +1 -0
- package/dist/esm/lib/bytesToUUID.js +1 -0
- package/dist/esm/lib/exportKeyPairs.js +1 -0
- package/dist/esm/lib/getRandomBytes.js +1 -0
- package/dist/esm/lib/getRandomValues.js +1 -0
- package/dist/esm/lib/uuidv4.js +1 -0
- package/dist/esm/lib/uuidv5.js +1 -0
- package/dist/esm/runtime/isNode.js +1 -0
- package/dist/index.d.ts +94 -0
- package/package.json +97 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kuzuki Azusa
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
# README\.md
|
|
2
|
+
|
|
3
|
+
# ka\-crypto
|
|
4
|
+
|
|
5
|
+
**Zero\-dependency cross\-platform RSA \+ AES hybrid encryption utility for Node\.js \& Browser, fully compatible with PHP RSA\-OAEP\-SHA1\.**
|
|
6
|
+
|
|
7
|
+
**GitHub:**[https://github\.com/Cirnotsuki/ka\-crypto](https://github.com/Cirnotsuki/ka-crypto)
|
|
8
|
+
|
|
9
|
+
Built on native Web Crypto / Node\.js Crypto API, no third\-party dependencies\. Lightweight and standard\-compliant\.
|
|
10
|
+
|
|
11
|
+
**Core advantage: 100% algorithm consistent with PHP openssl OAEP\-SHA1**, perfectly solving front\-end \& PHP backend encryption docking issues\.
|
|
12
|
+
|
|
13
|
+
## ✨ Features
|
|
14
|
+
|
|
15
|
+
- **Cross\-platform**: Support Node\.js \& all modern browsers
|
|
16
|
+
|
|
17
|
+
- **Zero dependency**: Only rely on system native crypto API
|
|
18
|
+
|
|
19
|
+
- **PHP full compatible**: RSA\-OAEP\-SHA1 strictly matches PHP openssl default OAEP mode
|
|
20
|
+
|
|
21
|
+
- **Secure hybrid encryption**: RSA key exchange \+ AES\-256\-GCM authenticated encryption
|
|
22
|
+
|
|
23
|
+
- **Tamper\-proof**: GCM auth tag ensures data integrity
|
|
24
|
+
|
|
25
|
+
- **Unified structure**: Fixed cipher format for front\-end \& PHP backend interaction
|
|
26
|
+
|
|
27
|
+
- **Standard key support**: Compatible with PKCS\#1 / PKCS\#8 PEM RSA keys
|
|
28
|
+
|
|
29
|
+
- **Independent crypto methods**: Expose standalone RSA / AES encrypt \& decrypt functions for flexible usage
|
|
30
|
+
|
|
31
|
+
## 🔐 Encryption Flow \(PHP Consistent\)
|
|
32
|
+
|
|
33
|
+
1. Randomly generate AES\-256\-GCM session key and IV
|
|
34
|
+
|
|
35
|
+
2. Encrypt plainData with AES\-256\-GCM, get ciphertext and auth tag
|
|
36
|
+
|
|
37
|
+
3. Encrypt AES session key via **RSA\-OAEP\-SHA1** \(PHP standard algorithm\)
|
|
38
|
+
|
|
39
|
+
4. Package all fields into unified cipher object for transmission
|
|
40
|
+
|
|
41
|
+
5. Decrypt: Restore AES key with RSA private key, then decrypt AES ciphertext
|
|
42
|
+
|
|
43
|
+
## 📦 Installation
|
|
44
|
+
|
|
45
|
+
```Plain Text
|
|
46
|
+
npm install ka-crypto
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## 🚀 Quick Usage
|
|
50
|
+
|
|
51
|
+
### 1\. Generate RSA Key Pairs
|
|
52
|
+
|
|
53
|
+
```js
|
|
54
|
+
import { keyPairs } from 'ka-crypto';
|
|
55
|
+
|
|
56
|
+
// Return RSA public key / private key (PEM format)
|
|
57
|
+
const [publicKey, privateKey] = await keyPairs();
|
|
58
|
+
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### 2\. Hybrid Encrypt \(RSA \+ AES\)
|
|
62
|
+
|
|
63
|
+
```js
|
|
64
|
+
import { encrypt } from 'ka-crypto';
|
|
65
|
+
|
|
66
|
+
// Param: plainData, RSA publicKey (PEM format)
|
|
67
|
+
const { data, valid } = await encrypt('any type of data', publicKey);
|
|
68
|
+
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### 3\. Hybrid Decrypt \(RSA \+ AES\)
|
|
72
|
+
|
|
73
|
+
```js
|
|
74
|
+
import { decrypt } from 'ka-crypto';
|
|
75
|
+
|
|
76
|
+
// Param: data, valid, RSA privateKey (PEM format)
|
|
77
|
+
const plainData = await decrypt(data, valid, privateKey);
|
|
78
|
+
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## 🧩 Standalone AES / RSA Methods
|
|
82
|
+
|
|
83
|
+
Support independent use of single encryption and decryption algorithm, flexible for custom business scenarios\.
|
|
84
|
+
|
|
85
|
+
### AES Encrypt / Decrypt \(AES\-256\-GCM\)
|
|
86
|
+
|
|
87
|
+
```js
|
|
88
|
+
import { aesEncrypt, aesDecrypt } from 'ka-crypto';
|
|
89
|
+
|
|
90
|
+
// AES encryption
|
|
91
|
+
const aesResult = aesEncrypt(plainData, aesKey, iv);
|
|
92
|
+
|
|
93
|
+
// AES decryption
|
|
94
|
+
const originText = aesDecrypt(cipherText, aesKey, iv, tag);
|
|
95
|
+
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### RSA Encrypt / Decrypt \(RSA\-OAEP\-SHA1\)
|
|
99
|
+
|
|
100
|
+
```js
|
|
101
|
+
import { rsaEncrypt, rsaDecrypt } from 'ka-crypto';
|
|
102
|
+
|
|
103
|
+
// RSA public key encryption
|
|
104
|
+
const rsaCipher = rsaEncrypt(plainData, publicKey);
|
|
105
|
+
|
|
106
|
+
// RSA private key decryption
|
|
107
|
+
const originData = rsaDecrypt(rsaCipher, privateKey);
|
|
108
|
+
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Random Bytes Generator
|
|
112
|
+
|
|
113
|
+
High\-quality pseudo\-random byte generation based on Mersenne Twister algorithm, used for custom IV / key random filling, consistent random logic across Node\.js and browsers\.
|
|
114
|
+
|
|
115
|
+
```js
|
|
116
|
+
import { getRandomValues } from 'ka-crypto';
|
|
117
|
+
|
|
118
|
+
// Fill Uint8Array with secure random bytes (0-255)
|
|
119
|
+
const buf = new Uint8Array(16);
|
|
120
|
+
getRandomValues(buf);
|
|
121
|
+
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
**Function Description**
|
|
125
|
+
|
|
126
|
+
- Based on Mersenne Twister pseudo\-random algorithm, stable and high randomness
|
|
127
|
+
|
|
128
|
+
- Cross\-environment consistency: unified random byte generation logic for browser and Node\.js
|
|
129
|
+
|
|
130
|
+
- Param: `Uint8Array` — Binary array to be filled with random bytes
|
|
131
|
+
|
|
132
|
+
- Param: `Uint8Array` — Binary array to be filled with random bytes
|
|
133
|
+
|
|
134
|
+
- Return: Filled original Uint8Array \(mutate in place\)
|
|
135
|
+
|
|
136
|
+
### UUID Generator \(RFC4122 Standard\)
|
|
137
|
+
|
|
138
|
+
Generate standard **RFC4122 Version 4 UUID**, based on internal Mersenne Twister random bytes, cross\-environment consistent and verifiable\.
|
|
139
|
+
|
|
140
|
+
```js
|
|
141
|
+
import { getUUID } from 'ka-crypto';
|
|
142
|
+
|
|
143
|
+
// Standard UUID (with dash)
|
|
144
|
+
const uuid = getUUID(false);
|
|
145
|
+
|
|
146
|
+
// Simplified UUID (no dash)
|
|
147
|
+
const simpleUuid = getUUID(true);
|
|
148
|
+
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
**Function Description**
|
|
152
|
+
|
|
153
|
+
- Strictly compliant with **RFC4122 v4 UUID** specification
|
|
154
|
+
|
|
155
|
+
- Random seed based on Mersenne Twister algorithm, uniform with crypto random logic
|
|
156
|
+
|
|
157
|
+
- Built\-in format verification, throws error if generated UUID is invalid
|
|
158
|
+
|
|
159
|
+
- Cross\-environment consistent output for Node\.js and browsers
|
|
160
|
+
|
|
161
|
+
**Parameters**
|
|
162
|
+
|
|
163
|
+
- `simplify: boolean`
|
|
164
|
+
|
|
165
|
+
- `false`\(default\): Return standard UUID with dashes `xxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx`
|
|
166
|
+
|
|
167
|
+
- `true`: Return pure 32\-bit hex string without dashes
|
|
168
|
+
|
|
169
|
+
**Return Value**
|
|
170
|
+
|
|
171
|
+
- `string`: Valid RFC4122 UUID string
|
|
172
|
+
|
|
173
|
+
**Exception**
|
|
174
|
+
|
|
175
|
+
- Throw `TypeError` when UUID format verification fails
|
|
176
|
+
|
|
177
|
+
## 🔑 Keypairs Export \(Node Environment Only\)
|
|
178
|
+
|
|
179
|
+
Built\-in RSA key pair automatic generation \& local file export function,**only available in Node\.js environment**, disabled in browsers \(browser prohibits local file writing\)\.
|
|
180
|
+
|
|
181
|
+
### Function Description
|
|
182
|
+
|
|
183
|
+
Quickly generate standard RSA PEM key pairs \(public key \+ private key\) and automatically write them to the specified local directory, convenient for project initialization and backend PHP key deployment\.
|
|
184
|
+
|
|
185
|
+
### Usage
|
|
186
|
+
|
|
187
|
+
```js
|
|
188
|
+
import { exportKeyPairs } from 'ka-crypto';
|
|
189
|
+
|
|
190
|
+
// Param: distPath (local folder path)
|
|
191
|
+
await exportKeyPairs('./keys');
|
|
192
|
+
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
### Parameter Explanation
|
|
196
|
+
|
|
197
|
+
- **distPath**: Local directory path for storing key files \(relative/absolute path supported\)
|
|
198
|
+
|
|
199
|
+
### Export Result
|
|
200
|
+
|
|
201
|
+
After successful execution, two standard PEM key files will be generated in the target directory:
|
|
202
|
+
|
|
203
|
+
- `public.pem`: RSA public key \(for frontend encryption / PHP public key encryption\)
|
|
204
|
+
|
|
205
|
+
- `private.pem`: RSA private key \(for backend decryption / JS private key decryption\)
|
|
206
|
+
|
|
207
|
+
### Important Notes
|
|
208
|
+
|
|
209
|
+
- ❌ **Unavailable in browsers**: Browser sandbox restricts local file system writing, calling this function in browser will throw an error
|
|
210
|
+
|
|
211
|
+
- ✅ **Only for Node\.js**: Suitable for local development, server initialization key generation
|
|
212
|
+
|
|
213
|
+
- Generated keys fully comply with **RSA\-OAEP\-SHA1** standard, natively compatible with PHP openssl encryption and decryption
|
|
214
|
+
|
|
215
|
+
## 📄 Cipher Structure \(JS \& PHP Interoperable\)
|
|
216
|
+
|
|
217
|
+
Unified transmission structure, directly JSON serializable for PHP backend docking:
|
|
218
|
+
|
|
219
|
+
```ts
|
|
220
|
+
interface CipherData {
|
|
221
|
+
data: string; // AES encrypted ciphertext (base64)
|
|
222
|
+
valid: {
|
|
223
|
+
key: string; // RSA-OAEP-SHA1 encrypted AES key (base64)
|
|
224
|
+
iv: string; // AES-256-GCM initialization vector (base64)
|
|
225
|
+
},
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
## 🛡️ Algorithm Standard
|
|
231
|
+
|
|
232
|
+
- **AES**: AES\-256\-GCM \(authenticated encryption, anti\-tampering\)
|
|
233
|
+
|
|
234
|
+
- **RSA**: RSA\-OAEP\-SHA1 \(fully compatible with PHP `openssl_public_encrypt` OAEP mode\)
|
|
235
|
+
|
|
236
|
+
- **Key Format**: Standard PEM public / private key
|
|
237
|
+
|
|
238
|
+
## 🌍 Compatibility
|
|
239
|
+
|
|
240
|
+
- Runtime: Node\.js 16\+, Chrome / Edge / Firefox / Safari latest
|
|
241
|
+
|
|
242
|
+
- Backend: PHP 7\.4\+ / PHP 8\.x \(openssl extension required\)
|
|
243
|
+
|
|
244
|
+
## 📝 License
|
|
245
|
+
|
|
246
|
+
MIT
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("fs"),r=require("path");function t(e){const r=e instanceof Uint8Array?e:new Uint8Array(e);if(void 0!==globalThis.Buffer)return globalThis.Buffer.from(r).toString("base64");let t="";for(let e=0;e<r.length;e+=32768)t+=String.fromCharCode(...r.subarray(e,e+32768));return btoa(t)}function n(){if("undefined"!=typeof window&&window.crypto)return window.crypto;if("undefined"!=typeof self&&self.crypto)return self.crypto;if("undefined"!=typeof globalThis&&globalThis.crypto)return globalThis.crypto;if("undefined"!=typeof window&&window.msCrypto)return window.msCrypto;if("undefined"!=typeof global&&global.crypto)return global.crypto;if("function"==typeof require)return require("crypto").webcrypto;throw new Error("WebCrypto not available")}function o(){const e=n().subtle;if(!e)throw new Error("SubtleCrypto not available");return e}async function a(){const e=await o().generateKey({name:"RSA-OAEP",modulusLength:2048,publicExponent:new Uint8Array([1,0,1]),hash:"SHA-1"},!0,["encrypt","decrypt"]);return["-----BEGIN PUBLIC KEY-----\n"+t(await o().exportKey("spki",e.publicKey))+"\n-----END PUBLIC KEY-----","-----BEGIN PRIVATE KEY-----\n"+t(await o().exportKey("pkcs8",e.privateKey))+"\n-----END PRIVATE KEY-----"]}function i(e){return n().getRandomValues(e)}function c(e=16){return i(new Uint8Array(e))}function s(e){return e.replace(/-----BEGIN [^-]+-----/g,"").replace(/-----END [^-]+-----/g,"").replace(/\s+/g,"")}function u(e){const r=s(e);if(void 0!==globalThis.Buffer){const e=globalThis.Buffer.from(r,"base64");return e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength)}const t=atob(r),n=new Uint8Array(t.length);for(let e=0;e<t.length;e++)n[e]=t.charCodeAt(e);return n.buffer}async function y(t){if("undefined"!=typeof process&&process.versions?.node){const[n,o]=await a();return e.writeFileSync(r.join(t,"public.pem"),n,"utf-8"),void e.writeFileSync(r.join(t,"private.pem"),o,"utf-8")}throw new Error("require Node runtime environment")}const p=Array.from({length:256},(e,r)=>r.toString(16).padStart(2,"0"));function f(e){const r=e;return p[r[0]]+p[r[1]]+p[r[2]]+p[r[3]]+"-"+p[r[4]]+p[r[5]]+"-"+p[r[6]]+p[r[7]]+"-"+p[r[8]]+p[r[9]]+"-"+p[r[10]]+p[r[11]]+p[r[12]]+p[r[13]]+p[r[14]]+p[r[15]]}function l(e=!1){const r=i(new Uint8Array(16));r[6]=15&r[6]|64,r[8]=63&r[8]|128;const t=f(r);return e?t.replace(/-/g,""):t}const d="6ba7b811-9dad-11d1-80b4-00c04fd430c8";async function w(e,r=d){const t=Uint8Array.from(r.replace(/-/g,"").match(/.{2}/g).map(e=>parseInt(e,16))),n=(new TextEncoder).encode(e),a=new Uint8Array(t.length+n.length);a.set(t,0),a.set(n,t.length);const i=await o().digest("SHA-1",a),c=new Uint8Array(i).slice(0,16);return c[6]=15&c[6]|80,c[8]=63&c[8]|128,f(c)}async function b(e,r=c(32),n=c(16)){const a=await o().importKey("raw",r,{name:"AES-GCM"},!1,["encrypt"]);return{data:t(await o().encrypt({name:"AES-GCM",iv:n},a,(new TextEncoder).encode(JSON.stringify(e)))),key:t(r),iv:t(n)}}async function g(e,r){const n=await async function(e){const r=u(e);return o().importKey("spki",r,{name:"RSA-OAEP",hash:"SHA-1"},!1,["encrypt"])}(r);return t(await o().encrypt({name:"RSA-OAEP"},n,(new TextEncoder).encode(JSON.stringify(e))))}async function A(e,r){if(!r)return null;const t=await b(e);return{valid:await g({key:t.key,iv:t.iv},r),data:t.data}}async function m(e,r,t){try{const n=await o().importKey("raw",u(r),{name:"AES-GCM"},!1,["decrypt"]),a=await o().decrypt({name:"AES-GCM",iv:u(t)},n,u(e));return JSON.parse((new TextDecoder).decode(a))}catch(e){return console.error(e),null}}async function E(e,r){const t=await async function(e){const r=u(e);return o().importKey("pkcs8",r,{name:"RSA-OAEP",hash:"SHA-1"},!1,["decrypt"])}(r),n=await o().decrypt({name:"RSA-OAEP"},t,u(e));return JSON.parse((new TextDecoder).decode(n))}async function h(e,r,t){try{const n=await E(r,t);return m(e,n.key,n.iv)}catch(e){return console.error(e),null}}var x=Object.freeze({__proto__:null,aesDecrypt:m,aesEncrypt:b,arrayBufferToBase64:t,base64Cleaner:s,base64ToArrayBuffer:u,decrypt:h,encrypt:A,exportKeyPairs:y,getRandomBytes:c,getRandomValues:i,keyPairs:a,rsaDecrypt:E,rsaEncrypt:g,uuidv4:l,uuidv5:w});exports.aesDecrypt=m,exports.aesEncrypt=b,exports.arrayBufferToBase64=t,exports.base64Cleaner=s,exports.base64ToArrayBuffer=u,exports.decrypt=h,exports.default=x,exports.encrypt=A,exports.exportKeyPairs=y,exports.getRandomBytes=c,exports.getRandomValues=i,exports.keyPairs=a,exports.rsaDecrypt=E,exports.rsaEncrypt=g,exports.uuidv4=l,exports.uuidv5=w;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function o(){if("undefined"!=typeof window&&window.crypto)return window.crypto;if("undefined"!=typeof self&&self.crypto)return self.crypto;if("undefined"!=typeof globalThis&&globalThis.crypto)return globalThis.crypto;if("undefined"!=typeof window&&window.msCrypto)return window.msCrypto;if("undefined"!=typeof global&&global.crypto)return global.crypto;if("function"==typeof require)return require("crypto").webcrypto;throw new Error("WebCrypto not available")}export{o as getCrypto};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{getCrypto as t}from"./getCrypto.js";function r(){const r=t().subtle;if(!r)throw new Error("SubtleCrypto not available");return r}export{r as getSubtle};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{base64ToArrayBuffer as r}from"../lib/base64ToArrayBuffer.js";import{getSubtle as t}from"./getSubtle.js";async function e(e){const o=r(e);return t().importKey("pkcs8",o,{name:"RSA-OAEP",hash:"SHA-1"},!1,["decrypt"])}export{e as importPrivateKey};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{base64ToArrayBuffer as r}from"../lib/base64ToArrayBuffer.js";import{getSubtle as t}from"./getSubtle.js";async function e(e){const o=r(e);return t().importKey("spki",o,{name:"RSA-OAEP",hash:"SHA-1"},!1,["encrypt"])}export{e as importPublicKey};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{arrayBufferToBase64 as e}from"../lib/arrayBufferToBase64.js";import{getSubtle as t}from"./getSubtle.js";async function r(){const r=await t().generateKey({name:"RSA-OAEP",modulusLength:2048,publicExponent:new Uint8Array([1,0,1]),hash:"SHA-1"},!0,["encrypt","decrypt"]);return["-----BEGIN PUBLIC KEY-----\n"+e(await t().exportKey("spki",r.publicKey))+"\n-----END PUBLIC KEY-----","-----BEGIN PRIVATE KEY-----\n"+e(await t().exportKey("pkcs8",r.privateKey))+"\n-----END PRIVATE KEY-----"]}export{r as keyPairs};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{base64ToArrayBuffer as r}from"../lib/base64ToArrayBuffer.js";import{getSubtle as e}from"../core/getSubtle.js";async function t(t,o,a){try{const c=await e().importKey("raw",r(o),{name:"AES-GCM"},!1,["decrypt"]),n=await e().decrypt({name:"AES-GCM",iv:r(a)},c,r(t));return JSON.parse((new TextDecoder).decode(n))}catch(r){return console.error(r),null}}export{t as aesDecrypt};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{arrayBufferToBase64 as e}from"../lib/arrayBufferToBase64.js";import{getSubtle as r}from"../core/getSubtle.js";import{getRandomBytes as t}from"../lib/getRandomBytes.js";async function o(o,a=t(32),n=t(16)){const i=await r().importKey("raw",a,{name:"AES-GCM"},!1,["encrypt"]),m=await r().encrypt({name:"AES-GCM",iv:n},i,(new TextEncoder).encode(JSON.stringify(o)));return{data:e(m),key:e(a),iv:e(n)}}export{o as aesEncrypt};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{aesDecrypt as r}from"./aesDecrypt.js";import{rsaDecrypt as t}from"./rsaDecrypt.js";async function o(o,e,c){try{const n=await t(e,c);return r(o,n.key,n.iv)}catch(r){return console.error(r),null}}export{o as decrypt};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{aesEncrypt as t}from"./aesEncrypt.js";import{rsaEncrypt as a}from"./rsaEncrypt.js";async function r(r,i){if(!i)return null;const n=await t(r);return{valid:await a({key:n.key,iv:n.iv},i),data:n.data}}export{r as encrypt};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{base64ToArrayBuffer as e}from"../lib/base64ToArrayBuffer.js";import{importPrivateKey as r}from"../core/importPrivateKey.js";import{getSubtle as o}from"../core/getSubtle.js";async function t(t,a){const i=await r(a),c=await o().decrypt({name:"RSA-OAEP"},i,e(t));return JSON.parse((new TextDecoder).decode(c))}export{t as rsaDecrypt};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{arrayBufferToBase64 as r}from"../lib/arrayBufferToBase64.js";import{importPublicKey as e}from"../core/importPublicKey.js";import{getSubtle as o}from"../core/getSubtle.js";async function t(t,i){const n=await e(i),a=await o().encrypt({name:"RSA-OAEP"},n,(new TextEncoder).encode(JSON.stringify(t)));return r(a)}export{t as rsaEncrypt};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { aesDecrypt } from "../cripter/aesDecrypt.js";export { aesDecrypt as default} from "../cripter/aesDecrypt.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { aesEncrypt } from "../cripter/aesEncrypt.js";export { aesEncrypt as default} from "../cripter/aesEncrypt.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { arrayBufferToBase64 } from "../lib/arrayBufferToBase64.js";export { arrayBufferToBase64 as default} from "../lib/arrayBufferToBase64.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { base64Cleaner } from "../lib/base64Cleaner.js";export { base64Cleaner as default} from "../lib/base64Cleaner.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { base64ToArrayBuffer } from "../lib/base64ToArrayBuffer.js";export { base64ToArrayBuffer as default} from "../lib/base64ToArrayBuffer.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { decrypt } from "../cripter/decrypt.js";export { decrypt as default} from "../cripter/decrypt.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { encrypt } from "../cripter/encrypt.js";export { encrypt as default} from "../cripter/encrypt.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { exportKeyPairs } from "../lib/exportKeyPairs.js";export { exportKeyPairs as default} from "../lib/exportKeyPairs.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { getRandomBytes } from "../lib/getRandomBytes.js";export { getRandomBytes as default} from "../lib/getRandomBytes.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { getRandomValues } from "../lib/getRandomValues.js";export { getRandomValues as default} from "../lib/getRandomValues.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { keyPairs } from "../core/keyPairs.js";export { keyPairs as default} from "../core/keyPairs.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { rsaDecrypt } from "../cripter/rsaDecrypt.js";export { rsaDecrypt as default} from "../cripter/rsaDecrypt.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { rsaEncrypt } from "../cripter/rsaEncrypt.js";export { rsaEncrypt as default} from "../cripter/rsaEncrypt.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { uuidv4 } from "../lib/uuidv4.js";export { uuidv4 as default} from "../lib/uuidv4.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { uuidv5 } from "../lib/uuidv5.js";export { uuidv5 as default} from "../lib/uuidv5.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as keyPairs } from "./exports/key-pairs";export { default as getRandomBytes } from "./exports/get-random-bytes";export { default as arrayBufferToBase64 } from "./exports/array-buffer-to-base64";export { default as base64ToArrayBuffer } from "./exports/base64-to-array-buffer";export { default as getRandomValues } from "./exports/get-random-values";export { default as exportKeyPairs } from "./exports/export-key-pairs";export { default as base64Cleaner } from "./exports/base64-cleaner";export { default as uuidv4 } from "./exports/uuidv4";export { default as uuidv5 } from "./exports/uuidv5";export { default as encrypt } from "./exports/encrypt";export { default as decrypt } from "./exports/decrypt";export { default as rsaEncrypt } from "./exports/rsa-encrypt";export { default as rsaDecrypt } from "./exports/rsa-decrypt";export { default as aesEncrypt } from "./exports/aes-encrypt";export { default as aesDecrypt } from "./exports/aes-decrypt";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import*as r from"./exports.js";export{aesDecrypt}from"./cripter/aesDecrypt.js";export{aesEncrypt}from"./cripter/aesEncrypt.js";export{arrayBufferToBase64}from"./lib/arrayBufferToBase64.js";export{base64Cleaner}from"./lib/base64Cleaner.js";export{base64ToArrayBuffer}from"./lib/base64ToArrayBuffer.js";export{decrypt}from"./cripter/decrypt.js";export{encrypt}from"./cripter/encrypt.js";export{exportKeyPairs}from"./lib/exportKeyPairs.js";export{getRandomBytes}from"./lib/getRandomBytes.js";export{getRandomValues}from"./lib/getRandomValues.js";export{keyPairs}from"./core/keyPairs.js";export{rsaDecrypt}from"./cripter/rsaDecrypt.js";export{rsaEncrypt}from"./cripter/rsaEncrypt.js";export{uuidv4}from"./lib/uuidv4.js";export{uuidv5}from"./lib/uuidv5.js";export{r as default};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function r(r){const t=r instanceof Uint8Array?r:new Uint8Array(r);if(void 0!==globalThis.Buffer)return globalThis.Buffer.from(t).toString("base64");let n="";for(let r=0;r<t.length;r+=32768)n+=String.fromCharCode(...t.subarray(r,r+32768));return btoa(n)}export{r as arrayBufferToBase64};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function e(e){return e.replace(/-----BEGIN [^-]+-----/g,"").replace(/-----END [^-]+-----/g,"").replace(/\s+/g,"")}export{e as base64Cleaner};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{base64Cleaner as e}from"./base64Cleaner.js";function t(t){const f=e(t);if(void 0!==globalThis.Buffer){const e=globalThis.Buffer.from(f,"base64");return e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength)}const r=atob(f),o=new Uint8Array(r.length);for(let e=0;e<r.length;e++)o[e]=r.charCodeAt(e);return o.buffer}export{t as base64ToArrayBuffer};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const t=Array.from({length:256},(t,r)=>r.toString(16).padStart(2,"0"));function r(r){const n=r;return t[n[0]]+t[n[1]]+t[n[2]]+t[n[3]]+"-"+t[n[4]]+t[n[5]]+"-"+t[n[6]]+t[n[7]]+"-"+t[n[8]]+t[n[9]]+"-"+t[n[10]]+t[n[11]]+t[n[12]]+t[n[13]]+t[n[14]]+t[n[15]]}export{r as bytesToUUID};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{keyPairs as r}from"../core/keyPairs.js";import{isNode as i}from"../runtime/isNode.js";import e from"fs";import o from"path";async function t(t){if(i()){const[i,n]=await r();return e.writeFileSync(o.join(t,"public.pem"),i,"utf-8"),void e.writeFileSync(o.join(t,"private.pem"),n,"utf-8")}throw new Error("require Node runtime environment")}export{t as exportKeyPairs};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{getRandomValues as r}from"./getRandomValues.js";function n(n=16){return r(new Uint8Array(n))}export{n as getRandomBytes};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{getCrypto as o}from"../core/getCrypto.js";function r(r){return o().getRandomValues(r)}export{r as getRandomValues};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{bytesToUUID as o}from"./bytesToUUID.js";import{getRandomValues as r}from"./getRandomValues.js";function t(t=!1){const e=r(new Uint8Array(16));e[6]=15&e[6]|64,e[8]=63&e[8]|128;const n=o(e);return t?n.replace(/-/g,""):n}export{t as uuidv4};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{getSubtle as t}from"../core/getSubtle.js";import{bytesToUUID as e}from"./bytesToUUID.js";const n={URL:"6ba7b811-9dad-11d1-80b4-00c04fd430c8"};async function r(r,o=n.URL){const c=Uint8Array.from(o.replace(/-/g,"").match(/.{2}/g).map(t=>parseInt(t,16))),a=(new TextEncoder).encode(r),s=new Uint8Array(c.length+a.length);s.set(c,0),s.set(a,c.length);const i=await t().digest("SHA-1",s),d=new Uint8Array(i).slice(0,16);return d[6]=15&d[6]|80,d[8]=63&d[8]|128,e(d)}export{n as UUID_NAMESPACE,r as uuidv5};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function e(){return"undefined"!=typeof process&&process.versions?.node}export{e as isNode};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
declare function keyPairs(): Promise<string[]>;
|
|
2
|
+
|
|
3
|
+
declare function getRandomBytes(length?: number): Uint8Array<ArrayBuffer>;
|
|
4
|
+
|
|
5
|
+
declare function arrayBufferToBase64(input: ArrayBuffer | Uint8Array): string;
|
|
6
|
+
|
|
7
|
+
declare function base64ToArrayBuffer(input: Base64URLString): ArrayBuffer;
|
|
8
|
+
|
|
9
|
+
declare function getRandomValues<T extends Exclude<BufferSource, ArrayBuffer>>(abv: T): T;
|
|
10
|
+
|
|
11
|
+
declare function exportKeyPairs(dist: string): Promise<void>;
|
|
12
|
+
|
|
13
|
+
declare function base64Cleaner(input: string): string;
|
|
14
|
+
|
|
15
|
+
declare function uuidv4(simplify?: boolean): string;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* UUID v5 generator
|
|
19
|
+
*/
|
|
20
|
+
declare function uuidv5(name: string, namespace?: string): Promise<string>;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 混合加密
|
|
24
|
+
*/
|
|
25
|
+
declare function encrypt<T>(data: T, publicKey: string): Promise<{
|
|
26
|
+
valid: string;
|
|
27
|
+
data: string;
|
|
28
|
+
} | null>;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 混合解密
|
|
32
|
+
*/
|
|
33
|
+
declare function decrypt(data: string, valid: string, privateKey: string): Promise<any>;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* RSA加密
|
|
37
|
+
*/
|
|
38
|
+
declare function rsaEncrypt<T>(data: T, publicKey: string): Promise<string>;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* RSA解密
|
|
42
|
+
*/
|
|
43
|
+
declare function rsaDecrypt(data: string, privateKey: string): Promise<any>;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* AES加密
|
|
47
|
+
*/
|
|
48
|
+
declare function aesEncrypt<T>(data: T, aesKey?: Uint8Array<ArrayBuffer>, aesIv?: Uint8Array<ArrayBuffer>): Promise<{
|
|
49
|
+
data: string;
|
|
50
|
+
key: string;
|
|
51
|
+
iv: string;
|
|
52
|
+
}>;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* AES解密
|
|
56
|
+
*/
|
|
57
|
+
declare function aesDecrypt(data: string, aesKey: string, aesIv: string): Promise<any>;
|
|
58
|
+
|
|
59
|
+
declare const KaCrypto_aesDecrypt: typeof aesDecrypt;
|
|
60
|
+
declare const KaCrypto_aesEncrypt: typeof aesEncrypt;
|
|
61
|
+
declare const KaCrypto_arrayBufferToBase64: typeof arrayBufferToBase64;
|
|
62
|
+
declare const KaCrypto_base64Cleaner: typeof base64Cleaner;
|
|
63
|
+
declare const KaCrypto_base64ToArrayBuffer: typeof base64ToArrayBuffer;
|
|
64
|
+
declare const KaCrypto_decrypt: typeof decrypt;
|
|
65
|
+
declare const KaCrypto_encrypt: typeof encrypt;
|
|
66
|
+
declare const KaCrypto_exportKeyPairs: typeof exportKeyPairs;
|
|
67
|
+
declare const KaCrypto_getRandomBytes: typeof getRandomBytes;
|
|
68
|
+
declare const KaCrypto_getRandomValues: typeof getRandomValues;
|
|
69
|
+
declare const KaCrypto_keyPairs: typeof keyPairs;
|
|
70
|
+
declare const KaCrypto_rsaDecrypt: typeof rsaDecrypt;
|
|
71
|
+
declare const KaCrypto_rsaEncrypt: typeof rsaEncrypt;
|
|
72
|
+
declare const KaCrypto_uuidv4: typeof uuidv4;
|
|
73
|
+
declare const KaCrypto_uuidv5: typeof uuidv5;
|
|
74
|
+
declare namespace KaCrypto {
|
|
75
|
+
export {
|
|
76
|
+
KaCrypto_aesDecrypt as aesDecrypt,
|
|
77
|
+
KaCrypto_aesEncrypt as aesEncrypt,
|
|
78
|
+
KaCrypto_arrayBufferToBase64 as arrayBufferToBase64,
|
|
79
|
+
KaCrypto_base64Cleaner as base64Cleaner,
|
|
80
|
+
KaCrypto_base64ToArrayBuffer as base64ToArrayBuffer,
|
|
81
|
+
KaCrypto_decrypt as decrypt,
|
|
82
|
+
KaCrypto_encrypt as encrypt,
|
|
83
|
+
KaCrypto_exportKeyPairs as exportKeyPairs,
|
|
84
|
+
KaCrypto_getRandomBytes as getRandomBytes,
|
|
85
|
+
KaCrypto_getRandomValues as getRandomValues,
|
|
86
|
+
KaCrypto_keyPairs as keyPairs,
|
|
87
|
+
KaCrypto_rsaDecrypt as rsaDecrypt,
|
|
88
|
+
KaCrypto_rsaEncrypt as rsaEncrypt,
|
|
89
|
+
KaCrypto_uuidv4 as uuidv4,
|
|
90
|
+
KaCrypto_uuidv5 as uuidv5,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export { aesDecrypt, aesEncrypt, arrayBufferToBase64, base64Cleaner, base64ToArrayBuffer, decrypt, KaCrypto as default, encrypt, exportKeyPairs, getRandomBytes, getRandomValues, keyPairs, rsaDecrypt, rsaEncrypt, uuidv4, uuidv5 };
|
package/package.json
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ka-libs/crypto",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Cross-environment crypto utility for Node.js & Browser, implement RSA-AES hybrid encryption based on native Web Crypto / Node.js crypto without third-party dependencies.",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"build:js": "rollup -c",
|
|
7
|
+
"build:types": "tsc -p tsconfig.types.json",
|
|
8
|
+
"build:clean": "node ./cleanDist.js",
|
|
9
|
+
"build": "npm run build:clean && npm run build:js"
|
|
10
|
+
},
|
|
11
|
+
"main": "./dist/cjs/index.cjs",
|
|
12
|
+
"module": "./dist/esm/index.js",
|
|
13
|
+
"browser": "dist/index.esm.js",
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"type": "module",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/esm/index.js",
|
|
20
|
+
"require": "./dist/index.cjs"
|
|
21
|
+
},
|
|
22
|
+
"./aes-decrypt": {
|
|
23
|
+
"import": "./dist/esm/exports/aes-decrypt.js"
|
|
24
|
+
},
|
|
25
|
+
"./aes-encrypt": {
|
|
26
|
+
"import": "./dist/esm/exports/aes-encrypt.js"
|
|
27
|
+
},
|
|
28
|
+
"./array-buffer-to-base64": {
|
|
29
|
+
"import": "./dist/esm/exports/array-buffer-to-base64.js"
|
|
30
|
+
},
|
|
31
|
+
"./base64-cleaner": {
|
|
32
|
+
"import": "./dist/esm/exports/base64-cleaner.js"
|
|
33
|
+
},
|
|
34
|
+
"./base64-to-array-buffer": {
|
|
35
|
+
"import": "./dist/esm/exports/base64-to-array-buffer.js"
|
|
36
|
+
},
|
|
37
|
+
"./decrypt": {
|
|
38
|
+
"import": "./dist/esm/exports/decrypt.js"
|
|
39
|
+
},
|
|
40
|
+
"./encrypt": {
|
|
41
|
+
"import": "./dist/esm/exports/encrypt.js"
|
|
42
|
+
},
|
|
43
|
+
"./export-key-pairs": {
|
|
44
|
+
"import": "./dist/esm/exports/export-key-pairs.js"
|
|
45
|
+
},
|
|
46
|
+
"./get-random-bytes": {
|
|
47
|
+
"import": "./dist/esm/exports/get-random-bytes.js"
|
|
48
|
+
},
|
|
49
|
+
"./get-random-values": {
|
|
50
|
+
"import": "./dist/esm/exports/get-random-values.js"
|
|
51
|
+
},
|
|
52
|
+
"./key-pairs": {
|
|
53
|
+
"import": "./dist/esm/exports/key-pairs.js"
|
|
54
|
+
},
|
|
55
|
+
"./rsa-decrypt": {
|
|
56
|
+
"import": "./dist/esm/exports/rsa-decrypt.js"
|
|
57
|
+
},
|
|
58
|
+
"./rsa-encrypt": {
|
|
59
|
+
"import": "./dist/esm/exports/rsa-encrypt.js"
|
|
60
|
+
},
|
|
61
|
+
"./uuidv4": {
|
|
62
|
+
"import": "./dist/esm/exports/uuidv4.js"
|
|
63
|
+
},
|
|
64
|
+
"./uuidv5": {
|
|
65
|
+
"import": "./dist/esm/exports/uuidv5.js"
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
"files": [
|
|
69
|
+
"dist",
|
|
70
|
+
"README.md",
|
|
71
|
+
"LICENSE"
|
|
72
|
+
],
|
|
73
|
+
"sideEffects": false,
|
|
74
|
+
"author": "Kuzuki Azusa",
|
|
75
|
+
"license": "MIT",
|
|
76
|
+
"keywords": [
|
|
77
|
+
"crypto",
|
|
78
|
+
"rsa",
|
|
79
|
+
"aes",
|
|
80
|
+
"hybrid-encrypt",
|
|
81
|
+
"web-crypto",
|
|
82
|
+
"node-crypto",
|
|
83
|
+
"cross-browser",
|
|
84
|
+
"cross-node",
|
|
85
|
+
"aes-gcm",
|
|
86
|
+
"rsa-oaep",
|
|
87
|
+
"encrypt",
|
|
88
|
+
"decrypt"
|
|
89
|
+
],
|
|
90
|
+
"devDependencies": {
|
|
91
|
+
"@types/node": "^26.0.1",
|
|
92
|
+
"typescript": "^5.8.3"
|
|
93
|
+
},
|
|
94
|
+
"publishConfig": {
|
|
95
|
+
"access": "public"
|
|
96
|
+
}
|
|
97
|
+
}
|