@ka-libs/crypto 1.2.0 → 1.3.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/README.md +17 -18
- package/dist/cjs/index.cjs +1 -1
- package/dist/esm/cripter/decrypt.js +1 -1
- package/dist/esm/cripter/encrypt.js +1 -1
- package/dist/esm/cripter/rsaDecrypt.js +1 -1
- package/dist/esm/cripter/rsaEncrypt.js +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/rsa-decrypt.d.ts +1 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
**Zero\-dependency cross\-platform RSA \+ AES hybrid encryption utility for Node\.js \& Browser, fully compatible with PHP RSA\-OAEP\-SHA1\.**
|
|
6
6
|
|
|
7
|
-
**GitHub:**[https://github\.com/Cirnotsuki/ka\-crypto](https://github.com/Cirnotsuki
|
|
7
|
+
**GitHub:**[https://github\.com/Cirnotsuki/ka\-crypto](https://github.com/Cirnotsuki/@ka-libs/crypto)
|
|
8
8
|
|
|
9
9
|
Built on native Web Crypto / Node\.js Crypto API, no third\-party dependencies\. Lightweight and standard\-compliant\.
|
|
10
10
|
|
|
@@ -43,7 +43,7 @@ Built on native Web Crypto / Node\.js Crypto API, no third\-party dependencies\.
|
|
|
43
43
|
## 📦 Installation
|
|
44
44
|
|
|
45
45
|
```Plain Text
|
|
46
|
-
npm install ka-crypto
|
|
46
|
+
npm install @ka-libs/crypto
|
|
47
47
|
```
|
|
48
48
|
|
|
49
49
|
## 🚀 Quick Usage
|
|
@@ -51,7 +51,7 @@ npm install ka-crypto
|
|
|
51
51
|
### 1\. Generate RSA Key Pairs
|
|
52
52
|
|
|
53
53
|
```js
|
|
54
|
-
import { keyPairs } from 'ka-crypto';
|
|
54
|
+
import { keyPairs } from '@ka-libs/crypto';
|
|
55
55
|
|
|
56
56
|
// Return RSA public key / private key (PEM format)
|
|
57
57
|
const [publicKey, privateKey] = await keyPairs();
|
|
@@ -61,7 +61,7 @@ const [publicKey, privateKey] = await keyPairs();
|
|
|
61
61
|
### 2\. Hybrid Encrypt \(RSA \+ AES\)
|
|
62
62
|
|
|
63
63
|
```js
|
|
64
|
-
import { encrypt } from 'ka-crypto';
|
|
64
|
+
import { encrypt } from '@ka-libs/crypto';
|
|
65
65
|
|
|
66
66
|
// Param: plainData, RSA publicKey (PEM format)
|
|
67
67
|
const { data, valid } = await encrypt('any type of data', publicKey);
|
|
@@ -71,7 +71,7 @@ const { data, valid } = await encrypt('any type of data', publicKey);
|
|
|
71
71
|
### 3\. Hybrid Decrypt \(RSA \+ AES\)
|
|
72
72
|
|
|
73
73
|
```js
|
|
74
|
-
import { decrypt } from 'ka-crypto';
|
|
74
|
+
import { decrypt } from '@ka-libs/crypto';
|
|
75
75
|
|
|
76
76
|
// Param: data, valid, RSA privateKey (PEM format)
|
|
77
77
|
const plainData = await decrypt(data, valid, privateKey);
|
|
@@ -85,20 +85,20 @@ Support independent use of single encryption and decryption algorithm, flexible
|
|
|
85
85
|
### AES Encrypt / Decrypt \(AES\-256\-GCM\)
|
|
86
86
|
|
|
87
87
|
```js
|
|
88
|
-
import { aesEncrypt, aesDecrypt } from 'ka-crypto';
|
|
88
|
+
import { aesEncrypt, aesDecrypt } from '@ka-libs/crypto';
|
|
89
89
|
|
|
90
90
|
// AES encryption
|
|
91
|
-
const aesResult = aesEncrypt(plainData
|
|
91
|
+
const aesResult = aesEncrypt(plainData);
|
|
92
92
|
|
|
93
93
|
// AES decryption
|
|
94
|
-
const originText = aesDecrypt(cipherText,
|
|
94
|
+
const originText = aesDecrypt(cipherText, payload);
|
|
95
95
|
|
|
96
96
|
```
|
|
97
97
|
|
|
98
98
|
### RSA Encrypt / Decrypt \(RSA\-OAEP\-SHA1\)
|
|
99
99
|
|
|
100
100
|
```js
|
|
101
|
-
import { rsaEncrypt, rsaDecrypt } from 'ka-crypto';
|
|
101
|
+
import { rsaEncrypt, rsaDecrypt } from '@ka-libs/crypto';
|
|
102
102
|
|
|
103
103
|
// RSA public key encryption
|
|
104
104
|
const rsaCipher = rsaEncrypt(plainData, publicKey);
|
|
@@ -113,7 +113,7 @@ const originData = rsaDecrypt(rsaCipher, privateKey);
|
|
|
113
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
114
|
|
|
115
115
|
```js
|
|
116
|
-
import { getRandomValues } from 'ka-crypto';
|
|
116
|
+
import { getRandomValues } from '@ka-libs/crypto';
|
|
117
117
|
|
|
118
118
|
// Fill Uint8Array with secure random bytes (0-255)
|
|
119
119
|
const buf = new Uint8Array(16);
|
|
@@ -138,13 +138,13 @@ getRandomValues(buf);
|
|
|
138
138
|
Generate standard **RFC4122 Version 4 UUID**, based on internal Mersenne Twister random bytes, cross\-environment consistent and verifiable\.
|
|
139
139
|
|
|
140
140
|
```js
|
|
141
|
-
import {
|
|
141
|
+
import { uuidv4 } from '@ka-libs/crypto';
|
|
142
142
|
|
|
143
143
|
// Standard UUID (with dash)
|
|
144
|
-
const uuid =
|
|
144
|
+
const uuid = uuidv4(false);
|
|
145
145
|
|
|
146
146
|
// Simplified UUID (no dash)
|
|
147
|
-
const simpleUuid =
|
|
147
|
+
const simpleUuid = uuidv4(true);
|
|
148
148
|
|
|
149
149
|
```
|
|
150
150
|
|
|
@@ -185,7 +185,7 @@ Quickly generate standard RSA PEM key pairs \(public key \+ private key\) and au
|
|
|
185
185
|
### Usage
|
|
186
186
|
|
|
187
187
|
```js
|
|
188
|
-
import { exportKeyPairs } from 'ka-crypto';
|
|
188
|
+
import { exportKeyPairs } from '@ka-libs/crypto';
|
|
189
189
|
|
|
190
190
|
// Param: distPath (local folder path)
|
|
191
191
|
await exportKeyPairs('./keys');
|
|
@@ -217,12 +217,11 @@ After successful execution, two standard PEM key files will be generated in the
|
|
|
217
217
|
Unified transmission structure, directly JSON serializable for PHP backend docking:
|
|
218
218
|
|
|
219
219
|
```ts
|
|
220
|
+
type AesPayload = Base64UrlString // Base64 from Combined ArrayBuffer: AesKey(32) + AesIv(12) + AesTag(16)
|
|
221
|
+
|
|
220
222
|
interface CipherData {
|
|
221
223
|
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
|
-
},
|
|
224
|
+
valid: string; // RSA-OAEP-SHA1 encrypted AES Payload (base64)
|
|
226
225
|
}
|
|
227
226
|
|
|
228
227
|
```
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("fs"),t=require("path");function r(e){const t=e instanceof Uint8Array?e:new Uint8Array(e);if(void 0!==globalThis.Buffer)return globalThis.Buffer.from(t).toString("base64");let r="";for(let e=0;e<t.length;e+=32768)r+=String.fromCharCode(...t.subarray(e,e+32768));return btoa(r)}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"+r(await o().exportKey("spki",e.publicKey))+"\n-----END PUBLIC KEY-----","-----BEGIN PRIVATE KEY-----\n"+r(await o().exportKey("pkcs8",e.privateKey))+"\n-----END PRIVATE KEY-----"]}function i(e){return n().getRandomValues(e)}function s(e=16){return i(new Uint8Array(e))}function c(e){return e.replace(/-----BEGIN [^-]+-----/g,"").replace(/-----END [^-]+-----/g,"").replace(/\s+/g,"")}function u(e){const t=c(e);if(void 0!==globalThis.Buffer){const e=globalThis.Buffer.from(t,"base64");return e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength)}const r=atob(t),n=new Uint8Array(r.length);for(let e=0;e<r.length;e++)n[e]=r.charCodeAt(e);return n.buffer}async function y(r){if("undefined"!=typeof process&&process.versions?.node){const[n,o]=await a();return e.writeFileSync(t.join(r,"public.pem"),n,"utf-8"),void e.writeFileSync(t.join(r,"private.pem"),o,"utf-8")}throw new Error("require Node runtime environment")}const p=Array.from({length:256},(e,t)=>t.toString(16).padStart(2,"0"));function f(e){const t=e;return p[t[0]]+p[t[1]]+p[t[2]]+p[t[3]]+"-"+p[t[4]]+p[t[5]]+"-"+p[t[6]]+p[t[7]]+"-"+p[t[8]]+p[t[9]]+"-"+p[t[10]]+p[t[11]]+p[t[12]]+p[t[13]]+p[t[14]]+p[t[15]]}function l(e=!1){const t=i(new Uint8Array(16));t[6]=15&t[6]|64,t[8]=63&t[8]|128;const r=f(t);return e?r.replace(/-/g,""):r}const d="6ba7b811-9dad-11d1-80b4-00c04fd430c8";async function w(e,t=d){const r=Uint8Array.from(t.replace(/-/g,"").match(/.{2}/g).map(e=>parseInt(e,16))),n=(new TextEncoder).encode(e),a=new Uint8Array(r.length+n.length);a.set(r,0),a.set(n,r.length);const i=await o().digest("SHA-1",a),s=new Uint8Array(i).slice(0,16);return s[6]=15&s[6]|80,s[8]=63&s[8]|128,f(s)}async function g(e,t=s(32),n=s(12)){const a=await o().importKey("raw",t,{name:"AES-GCM"},!1,["encrypt"]),i=await o().encrypt({name:"AES-GCM",iv:n,tagLength:128},a,(new TextEncoder).encode(JSON.stringify(e))),c=new Uint8Array(i),u=i.slice(c.length-16),y=new Uint8Array(t.length+n.length+16);return y.set(t,0),y.set(n,t.length),y.set(new Uint8Array(u),t.length+n.length),{data:r(i.slice(0,c.length-16)),payload:r(y)}}async function h(e,t){const n=await async function(e){const t=u(e);return o().importKey("spki",t,{name:"RSA-OAEP",hash:"SHA-1"},!1,["encrypt"])}(t);return r(await o().encrypt({name:"RSA-OAEP"},n,
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("fs"),t=require("path");function r(e){const t=e instanceof Uint8Array?e:new Uint8Array(e);if(void 0!==globalThis.Buffer)return globalThis.Buffer.from(t).toString("base64");let r="";for(let e=0;e<t.length;e+=32768)r+=String.fromCharCode(...t.subarray(e,e+32768));return btoa(r)}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"+r(await o().exportKey("spki",e.publicKey))+"\n-----END PUBLIC KEY-----","-----BEGIN PRIVATE KEY-----\n"+r(await o().exportKey("pkcs8",e.privateKey))+"\n-----END PRIVATE KEY-----"]}function i(e){return n().getRandomValues(e)}function s(e=16){return i(new Uint8Array(e))}function c(e){return e.replace(/-----BEGIN [^-]+-----/g,"").replace(/-----END [^-]+-----/g,"").replace(/\s+/g,"")}function u(e){const t=c(e);if(void 0!==globalThis.Buffer){const e=globalThis.Buffer.from(t,"base64");return e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength)}const r=atob(t),n=new Uint8Array(r.length);for(let e=0;e<r.length;e++)n[e]=r.charCodeAt(e);return n.buffer}async function y(r){if("undefined"!=typeof process&&process.versions?.node){const[n,o]=await a();return e.writeFileSync(t.join(r,"public.pem"),n,"utf-8"),void e.writeFileSync(t.join(r,"private.pem"),o,"utf-8")}throw new Error("require Node runtime environment")}const p=Array.from({length:256},(e,t)=>t.toString(16).padStart(2,"0"));function f(e){const t=e;return p[t[0]]+p[t[1]]+p[t[2]]+p[t[3]]+"-"+p[t[4]]+p[t[5]]+"-"+p[t[6]]+p[t[7]]+"-"+p[t[8]]+p[t[9]]+"-"+p[t[10]]+p[t[11]]+p[t[12]]+p[t[13]]+p[t[14]]+p[t[15]]}function l(e=!1){const t=i(new Uint8Array(16));t[6]=15&t[6]|64,t[8]=63&t[8]|128;const r=f(t);return e?r.replace(/-/g,""):r}const d="6ba7b811-9dad-11d1-80b4-00c04fd430c8";async function w(e,t=d){const r=Uint8Array.from(t.replace(/-/g,"").match(/.{2}/g).map(e=>parseInt(e,16))),n=(new TextEncoder).encode(e),a=new Uint8Array(r.length+n.length);a.set(r,0),a.set(n,r.length);const i=await o().digest("SHA-1",a),s=new Uint8Array(i).slice(0,16);return s[6]=15&s[6]|80,s[8]=63&s[8]|128,f(s)}async function g(e,t=s(32),n=s(12)){const a=await o().importKey("raw",t,{name:"AES-GCM"},!1,["encrypt"]),i=await o().encrypt({name:"AES-GCM",iv:n,tagLength:128},a,(new TextEncoder).encode(JSON.stringify(e))),c=new Uint8Array(i),u=i.slice(c.length-16),y=new Uint8Array(t.length+n.length+16);return y.set(t,0),y.set(n,t.length),y.set(new Uint8Array(u),t.length+n.length),{data:r(i.slice(0,c.length-16)),payload:r(y)}}async function h(e,t){const n=await async function(e){const t=u(e);return o().importKey("spki",t,{name:"RSA-OAEP",hash:"SHA-1"},!1,["encrypt"])}(t);let a;a=e instanceof ArrayBuffer?e:(new TextEncoder).encode(JSON.stringify(e));return r(await o().encrypt({name:"RSA-OAEP"},n,a))}async function A(e,t){if(!t)return null;const r=await g(e);return{valid:await h(u(r.payload),t),data:r.data}}async function b(e,t){try{const r=u(t),n=r.slice(0,32),a=r.slice(32,44),i=r.slice(-16),s=await o().importKey("raw",n,{name:"AES-GCM"},!1,["decrypt"]),c=new Uint8Array(u(e)),y=new Uint8Array(c.length+16);y.set(c,0),y.set(new Uint8Array(i),c.length);const p=await o().decrypt({name:"AES-GCM",iv:a,tagLength:128},s,y.buffer);return JSON.parse((new TextDecoder).decode(p))}catch(e){return console.error(e),null}}async function m(e,t,n=!1){const a=await async function(e){const t=u(e);return o().importKey("pkcs8",t,{name:"RSA-OAEP",hash:"SHA-1"},!1,["decrypt"])}(t),i=await o().decrypt({name:"RSA-OAEP"},a,u(e));return n?r(i):JSON.parse((new TextDecoder).decode(i))}async function E(e,t,r){try{return b(e,await m(t,r,!0))}catch(e){return console.error(e),null}}var x=Object.freeze({__proto__:null,aesDecrypt:b,aesEncrypt:g,arrayBufferToBase64:r,base64Cleaner:c,base64ToArrayBuffer:u,decrypt:E,encrypt:A,exportKeyPairs:y,getRandomBytes:s,getRandomValues:i,keyPairs:a,rsaDecrypt:m,rsaEncrypt:h,uuidv4:l,uuidv5:w});exports.aesDecrypt=b,exports.aesEncrypt=g,exports.arrayBufferToBase64=r,exports.base64Cleaner=c,exports.base64ToArrayBuffer=u,exports.decrypt=E,exports.default=x,exports.encrypt=A,exports.exportKeyPairs=y,exports.getRandomBytes=s,exports.getRandomValues=i,exports.keyPairs=a,exports.rsaDecrypt=m,exports.rsaEncrypt=h,exports.uuidv4=l,exports.uuidv5=w;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{aesDecrypt as r}from"./aesDecrypt.js";import{rsaDecrypt as t}from"./rsaDecrypt.js";async function o(o,c,e){try{const n=await t(c,e);return r(o,n)}catch(r){return console.error(r),null}}export{o as decrypt};
|
|
1
|
+
import{aesDecrypt as r}from"./aesDecrypt.js";import{rsaDecrypt as t}from"./rsaDecrypt.js";async function o(o,c,e){try{const n=await t(c,e,!0);return r(o,n)}catch(r){return console.error(r),null}}export{o as decrypt};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{aesEncrypt as a}from"./aesEncrypt.js";import{rsaEncrypt as t}from"./rsaEncrypt.js";async function
|
|
1
|
+
import{base64ToArrayBuffer as r}from"../lib/base64ToArrayBuffer.js";import{aesEncrypt as a}from"./aesEncrypt.js";import{rsaEncrypt as t}from"./rsaEncrypt.js";async function o(o,i){if(!i)return null;const n=await a(o);return{valid:await t(r(n.payload),i),data:n.data}}export{o as encrypt};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{base64ToArrayBuffer as
|
|
1
|
+
import{base64ToArrayBuffer as r}from"../lib/base64ToArrayBuffer.js";import{importPrivateKey as e}from"../core/importPrivateKey.js";import{getSubtle as o}from"../core/getSubtle.js";import{arrayBufferToBase64 as t}from"../lib/arrayBufferToBase64.js";import"fs";import"path";import"../lib/bytesToUUID.js";async function i(i,a,m=!1){const p=await e(a),s=await o().decrypt({name:"RSA-OAEP"},p,r(i));return m?t(s):JSON.parse((new TextDecoder).decode(s))}export{i as rsaDecrypt};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{arrayBufferToBase64 as r}from"../lib/arrayBufferToBase64.js";import{importPublicKey as e}from"../core/importPublicKey.js";import{getSubtle as
|
|
1
|
+
import{arrayBufferToBase64 as r}from"../lib/arrayBufferToBase64.js";import{importPublicKey as e}from"../core/importPublicKey.js";import{getSubtle as t}from"../core/getSubtle.js";async function o(o,n){const i=await e(n);let a;a=o instanceof ArrayBuffer?o:(new TextEncoder).encode(JSON.stringify(o));const c=await t().encrypt({name:"RSA-OAEP"},i,a);return r(c)}export{o as rsaEncrypt};
|
package/dist/types/index.d.ts
CHANGED
|
@@ -40,6 +40,7 @@ declare function rsaEncrypt<T>(data: T, publicKey: string): Promise<string>;
|
|
|
40
40
|
/**
|
|
41
41
|
* RSA解密
|
|
42
42
|
*/
|
|
43
|
+
declare function rsaDecrypt(data: string, privateKey: string, sourceIsBuffer: boolean): Promise<Base64URLString>;
|
|
43
44
|
declare function rsaDecrypt(data: string, privateKey: string): Promise<any>;
|
|
44
45
|
|
|
45
46
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ka-libs/crypto",
|
|
3
|
-
"version": "1.2
|
|
3
|
+
"version": "1.3.2",
|
|
4
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
5
|
"scripts": {
|
|
6
6
|
"build:js": "rollup -c",
|
|
@@ -109,4 +109,4 @@
|
|
|
109
109
|
"publishConfig": {
|
|
110
110
|
"access": "public"
|
|
111
111
|
}
|
|
112
|
-
}
|
|
112
|
+
}
|