@ka-libs/crypto 1.2.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.
- 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/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.
|
|
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
|
+
}
|