npm package, cjs -> esm...
This commit is contained in:
15 files changed
+410
-292
No files matched your search
Vendored
BIN
Binary file not shown.
Vendored
+1
@@ -0,0 +1 @@
|
||||
(()=>{var e={724:function(e,t,n){!function(e,t){"use strict";function r(e,t){if(!e)throw new Error(t||"Assertion failed")}function i(e,t){e.super_=t;var n=function(){};n.prototype=t.prototype,e.prototype=new n,e.prototype.constructor=e}function a(e,t,n){if(a.isBN(e))return e;this.negative=0,this.words=null,this.length=0,this.red=null,null!==e&&("le"!==t&&"be"!==t||(n=t,t=10),this._init(e||0,t||10,n||"be"))}var f;"object"==typeof e?e.exports=a:t.BN=a,a.BN=a,a.wordSize=26;try{f="undefined"!=typeof window&&void 0!==window.Buffer?window.Buffer:n(654).Buffer}catch(e){}function d(e,t){var n=e.charCodeAt(t);return n>=65&&n<=70?n-55:n>=97&&n<=102?n-87:n-48&15}function o(e,t,n){var r=d(e,n);return n-1>=t&&(r|=d(e,n-1)<<4),r}function s(e,t,n,r){for(var i=0,a=Math.min(e.length,n),f=t;f<a;f++){var d=e.charCodeAt(f)-48;i*=r,i+=d>=49?d-49+10:d>=17?d-17+10:d}return i}a.isBN=function(e){return e instanceof a||null!==e&&"object"==typeof e&&e.constructor.wordSize===a.wordSize&&Array.isArray(e.words)},a.max=function(e,t){return e.cmp(t)>0?e:t},a.min=function(e,t){return e.cmp(t)<0?e:t},a.prototype._init=function(e,t,n){if("number"==typeof e)return this._initNumber(e,t,n);if("object"==typeof e)return this._initArray(e,t,n);"hex"===t&&(t=16),r(t===(0|t)&&t>=2&&t<=36);var i=0;"-"===(e=e.toString().replace(/\s+/g,""))[0]&&(i++,this.negative=1),i<e.length&&(16===t?this._parseHex(e,i,n):(this._parseBase(e,t,i),"le"===n&&this._initArray(this.toArray(),t,n)))},a.prototype._initNumber=function(e,t,n){e<0&&(this.negative=1,e=-e),e<67108864?(this.words=[67108863&e],this.length=1):e<4503599627370496?(this.words=[67108863&e,e/67108864&67108863],this.length=2):(r(e<9007199254740992),this.words=[67108863&e,e/67108864&67108863,1],this.length=3),"le"===n&&this._initArray(this.toArray(),t,n)},a.prototype._initArray=function(e,t,n){if(r("number"==typeof e.length),e.length<=0)return this.words=[0],this.length=1,this;this.length=Math.ceil(e.length/3),this.words=new Array(this.length);for(var i=0;i<this.length;i++)this.words[i]=0;var a,f,d=0;if("be"===n)for(i=e.length-1,a=0;i>=0;i-=3)f=e[i]|e[i-1]<<8|e[i-2]<<16,this.words[a]|=f<<d&67108863,this.words[a+1]=f>>>26-d&67108863,(d+=24)>=26&&(d-=26,a++);else if("le"===n)for(i=0,a=0;i<e.length;i+=3)f=e[i]|e[i+1]<<8|e[i+2]<<16,this.words[a]|=f<<d&67108863,this.words[a+1]=f>>>26-d&67108863,(d+=24)>=26&&(d-=26,a++);return this.strip()},a.prototype._parseHex=function(e,t,n){this.length=Math.ceil((e.length-t)/6),this.words=new Array(this.length);for(var r=0;r<this.length;r++)this.words[r]=0;var i,a=0,f=0;if("be"===n)for(r=e.length-1;r>=t;r-=2)i=o(e,t,r)<<a,this.words[f]|=67108863&i,a>=18?(a-=18,f+=1,this.words[f]|=i>>>26):a+=8;else for(r=(e.length-t)%2==0?t+1:t;r<e.length;r+=2)i=o(e,t,r)<<a,this.words[f]|=67108863&i,a>=18?(a-=18,f+=1,this.words[f]|=i>>>26):a+=8;this.strip()},a.prototype._parseBase=function(e,t,n){this.words=[0],this.length=1;for(var r=0,i=1;i<=67108863;i*=t)r++;r--,i=i/t|0;for(var a=e.length-n,f=a%r,d=Math.min(a,a-f)+n,o=0,c=n;c<d;c+=r)o=s(e,c,c+r,t),this.imuln(i),this.words[0]+o<67108864?this.words[0]+=o:this._iaddn(o);if(0!==f){var h=1;for(o=s(e,c,e.length,t),c=0;c<f;c++)h*=t;this.imuln(h),this.words[0]+o<67108864?this.words[0]+=o:this._iaddn(o)}this.strip()},a.prototype.copy=function(e){e.words=new Array(this.length);for(var t=0;t<this.length;t++)e.words[t]=this.words[t];e.length=this.length,e.negative=this.negative,e.red=this.red},a.prototype.clone=function(){var e=new a(null);return this.copy(e),e},a.prototype._expand=function(e){for(;this.length<e;)this.words[this.length++]=0;return this},a.prototype.strip=function(){for(;this.length>1&&0===this.words[this.length-1];)this.length--;return this._normSign()},a.prototype._normSign=function(){return 1===this.length&&0===this.words[0]&&(this.negative=0),this},a.prototype.inspect=function(){return(this.red?"<BN-R: ":"<BN: ")+this.toString(16)+">"};var c=["","0","00","000","0000","00000","000000","0000000","00000000","000000000","0000000000","00000000000","000000000000","0000000000000","00000000000000","000000000000000","0000000000000000","00000000000000000","000000000000000000","0000000000000000000","00000000000000000000","000000000000000000000","0000000000000000000000","00000000000000000000000","000000000000000000000000","0000000000000000000000000"],h=[0,0,25,16,12,11,10,9,8,8,7,7,7,7,6,6,6,6,6,6,6,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5],u=[0,0,33554432,43046721,16777216,48828125,60466176,40353607,16777216,43046721,1e7,19487171,35831808,62748517,7529536,11390625,16777216,24137569,34012224,47045881,64e6,4084101,5153632,6436343,7962624,9765625,11881376,14348907,17210368,20511149,243e5,28629151,33554432,39135393,45435424,52521875,60466176];function b(e,t,n){n.negative=t.negative^e.negative;var r=e.length+t.length|0;n.length=r,r=r-1|0;var i=0|e.words[0],a=0|t.words[0],f=i*a,d=67108863&f,o=f/67108864|0;n.words[0]=d;for(var s=1;s<r;s++){for(var c=o>>>26,h=67108863&o,u=Math.min(s,t.length-1),b=Math.max(0,s-e.length+1);b<=u;b++){var l=s-b|0;c+=(f=(i=0|e.woLine truncated
|
||||
Vendored
+18
@@ -0,0 +1,18 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<title>ZKL Crypto Provider Demo</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>ZKL Crypto Provider Demo</h1>
|
||||
<p>Upload a file to be encrypted. The ciphertext will be presented as a bitmap here.</p>
|
||||
<input type="file" id="fileInput">
|
||||
<canvas id="bitmapCanvas"></canvas>
|
||||
<hr>
|
||||
<p>Yigid BALABAN, <a href="https://fybx.dev/">https://fybx.dev/</a></p>
|
||||
<script src="bundle.js"></script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
Vendored
+83
@@ -0,0 +1,83 @@
|
||||
import { generateKeypair } from "@zklx/kds";
|
||||
import {
|
||||
encryptFile,
|
||||
decryptFile,
|
||||
encryptString,
|
||||
decryptString,
|
||||
} from "../crypto.js";
|
||||
|
||||
let keypair;
|
||||
const el_fileInput = document.querySelector("#fileInput");
|
||||
|
||||
el_fileInput.addEventListener("change", function (event) {
|
||||
const file = event.target.files[0];
|
||||
if (file) {
|
||||
const reader = new FileReader();
|
||||
|
||||
reader.onload = function (e) {
|
||||
const arrayBuffer = e.target.result;
|
||||
const byteArray = new Uint8Array(arrayBuffer);
|
||||
if (!keypair) {
|
||||
console.error("keypair not ready, skipping file enc/dec");
|
||||
return;
|
||||
}
|
||||
|
||||
console.log("File to be encrypted:", el_fileInput.files[0].name);
|
||||
const cipherFile = encryptFile(
|
||||
keypair.pkey,
|
||||
el_fileInput.files[0].name,
|
||||
byteArray,
|
||||
);
|
||||
console.log("Ciphertext bytes:", cipherFile);
|
||||
|
||||
{
|
||||
const numPixels = cipherFile.length / 4;
|
||||
const width = Math.floor(Math.sqrt(numPixels));
|
||||
const height = Math.ceil(numPixels / width);
|
||||
|
||||
const canvas = document.getElementById("bitmapCanvas");
|
||||
const context = canvas.getContext("2d");
|
||||
|
||||
canvas.width = width;
|
||||
canvas.height = height;
|
||||
|
||||
const imageData = context.createImageData(width, height);
|
||||
|
||||
for (let i = 0; i < cipherFile.length; i++) {
|
||||
imageData.data[i] = cipherFile[i];
|
||||
}
|
||||
|
||||
context.putImageData(imageData, 0, 0);
|
||||
}
|
||||
|
||||
const plainFile = decryptFile(keypair.skey, cipherFile);
|
||||
console.log("Decrypted raw data:", plainFile.data);
|
||||
console.log(
|
||||
"Decrypted decoded:",
|
||||
new TextDecoder().decode(plainFile.data),
|
||||
);
|
||||
console.log("Decrypted file name:", plainFile.fileName);
|
||||
};
|
||||
|
||||
reader.readAsArrayBuffer(file);
|
||||
} else {
|
||||
console.warn("No file selected");
|
||||
}
|
||||
});
|
||||
|
||||
// we wait for a second before executing this block
|
||||
// because WASM module takes time to load
|
||||
setTimeout(async () => {
|
||||
console.log("start");
|
||||
const mnemonic =
|
||||
"digital radio analyst fine casino have mass blood potato hat web capital prefer debate fee differ spray cloud";
|
||||
|
||||
// for this time, skey means private key, and pkey is public
|
||||
const { publicKey: pkey, privateKey: skey } = await generateKeypair(mnemonic);
|
||||
keypair = { pkey, skey };
|
||||
|
||||
const cipherText = encryptString(pkey, "message");
|
||||
console.log("encrypted string is", cipherText);
|
||||
console.log("decrypted string is", decryptString(skey, cipherText));
|
||||
console.log("end");
|
||||
}, 1000);
|
||||
Reference in new issue
Block a user