@ka-libs/crypto 1.1.0 → 1.3.1

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.
@@ -1 +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;
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{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};
1
+ import{base64ToArrayBuffer as e}from"../lib/base64ToArrayBuffer.js";import{getSubtle as r}from"../core/getSubtle.js";async function t(t,n){try{const a=e(n),o=a.slice(0,32),c=a.slice(32,44),i=a.slice(-16),s=await r().importKey("raw",o,{name:"AES-GCM"},!1,["decrypt"]),l=new Uint8Array(e(t)),y=new Uint8Array(l.length+16);y.set(l,0),y.set(new Uint8Array(i),l.length);const f=await r().decrypt({name:"AES-GCM",iv:c,tagLength:128},s,y.buffer);return JSON.parse((new TextDecoder).decode(f))}catch(e){return console.error(e),null}}export{t as aesDecrypt};
@@ -1 +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};
1
+ import{arrayBufferToBase64 as e}from"../lib/arrayBufferToBase64.js";import{getSubtle as t}from"../core/getSubtle.js";import{getRandomBytes as n}from"../lib/getRandomBytes.js";async function r(r,a=n(32),i=n(12)){const o=await t().importKey("raw",a,{name:"AES-GCM"},!1,["encrypt"]),l=await t().encrypt({name:"AES-GCM",iv:i,tagLength:128},o,(new TextEncoder).encode(JSON.stringify(r))),s=new Uint8Array(l),g=l.slice(s.length-16),y=new Uint8Array(a.length+i.length+16);return y.set(a,0),y.set(i,a.length),y.set(new Uint8Array(g),a.length+i.length),{data:e(l.slice(0,s.length-16)),payload:e(y)}}export{r as aesEncrypt};
@@ -1 +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};
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 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};
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 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};
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 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};
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};
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * AES解密
3
3
  */
4
- declare function aesDecrypt(data: string, aesKey: string, aesIv: string): Promise<any>;
4
+ declare function aesDecrypt(data: string, payload: string): Promise<any>;
5
5
 
6
6
  export { aesDecrypt, aesDecrypt as default };
@@ -3,8 +3,7 @@
3
3
  */
4
4
  declare function aesEncrypt<T>(data: T, aesKey?: Uint8Array<ArrayBuffer>, aesIv?: Uint8Array<ArrayBuffer>): Promise<{
5
5
  data: string;
6
- key: string;
7
- iv: string;
6
+ payload: string;
8
7
  }>;
9
8
 
10
9
  export { aesEncrypt, aesEncrypt as default };
@@ -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
  /**
@@ -47,14 +48,13 @@ declare function rsaDecrypt(data: string, privateKey: string): Promise<any>;
47
48
  */
48
49
  declare function aesEncrypt<T>(data: T, aesKey?: Uint8Array<ArrayBuffer>, aesIv?: Uint8Array<ArrayBuffer>): Promise<{
49
50
  data: string;
50
- key: string;
51
- iv: string;
51
+ payload: string;
52
52
  }>;
53
53
 
54
54
  /**
55
55
  * AES解密
56
56
  */
57
- declare function aesDecrypt(data: string, aesKey: string, aesIv: string): Promise<any>;
57
+ declare function aesDecrypt(data: string, payload: string): Promise<any>;
58
58
 
59
59
  declare const KaCrypto_aesDecrypt: typeof aesDecrypt;
60
60
  declare const KaCrypto_aesEncrypt: typeof aesEncrypt;
@@ -1,6 +1,7 @@
1
1
  /**
2
2
  * RSA解密
3
3
  */
4
+ declare function rsaDecrypt(data: string, privateKey: string, sourceIsBuffer: boolean): Promise<Base64URLString>;
4
5
  declare function rsaDecrypt(data: string, privateKey: string): Promise<any>;
5
6
 
6
7
  export { rsaDecrypt as default, rsaDecrypt };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ka-libs/crypto",
3
- "version": "1.1.0",
3
+ "version": "1.3.1",
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
+ }