wilcocrypt 2.1.0 → 2.1.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 +0,0 @@
1
- import{createCipheriv as t,createDecipheriv as e,randomBytes as s,scryptSync as r}from"crypto";import{gzipSync as f,gunzipSync as n}from"zlib";import{readFileSync as i,writeFileSync as o}from"fs";var h,a,u,c,l,p,d,b={};function E(){if(p)return l;p=1;const t=new(function(){if(c)return u;c=1;const t=process.env.NOTEPACK_DECODE_KEY_CACHE_MAX_SIZE||1024,e=process.env.NOTEPACK_DECODE_KEY_CACHE_MAX_LENGTH||16;return u=class{constructor({maxSize:s=t,maxLength:r=e}={}){this.size=0,this.maxSize=s,this.maxLength=r,this.cache=new Map;for(let t=1;t<=this.maxLength;t++)this.cache.set(t,new Map)}get(t,e,s){if(s>this.maxLength)return!1;let r=this.cache.get(s);for(let f=0;f<s;f++){const s=t[e+f];if(!r.has(s))return!1;r=r.get(s)}return r}set(t,e,s,r){if(s>this.maxLength||this.size>=this.maxSize)return;this.size++;let f=this.cache.get(s);for(let n=0;n<s;n++){const i=t[e+n];if(n===s-1)f.set(i,r);else if(f.has(i))f=f.get(i);else{const t=new Map;f.set(i,t),f=t}}}}}());function e(t){this.offset=0,this.buffer=t,this.useKeyCache=!1}return e.prototype.array=function(t){const e=new Array(t);for(let s=0;s<t;s++)e[s]=this.parse();return e},e.prototype.map=function(t){let e="",s={};for(let r=0;r<t;r++)this.useKeyCache=!0,e=this.parse(),this.useKeyCache=!1,s[e]=this.parse();return s},e.prototype.str=function(e){if(this.useKeyCache){const s=t.get(this.buffer,this.offset,e);if(s)return this.offset+=e,s}const s=this.buffer.toString("utf8",this.offset,this.offset+e);return this.useKeyCache&&t.set(this.buffer,this.offset,e,s),this.offset+=e,s},e.prototype.bin=function(t){const e=this.buffer.slice(this.offset,this.offset+t);return this.offset+=t,e},e.prototype.arraybuffer=function(t){const e=new ArrayBuffer(t),s=new Uint8Array(e);for(let e=0;e<t;e++)s[e]=this.buffer[this.offset+e];return this.offset+=t,e},e.prototype.parse=function(){const t=this.buffer[this.offset++];let e,s=0,r=0,f=0,n=0;if(t<192)return t<128?t:t<144?this.map(15&t):t<160?this.array(15&t):this.str(31&t);if(t>223)return-1*(255-t+1);switch(t){case 192:return null;case 194:return!1;case 195:return!0;case 196:return s=this.buffer.readUInt8(this.offset),this.offset+=1,this.bin(s);case 197:return s=this.buffer.readUInt16BE(this.offset),this.offset+=2,this.bin(s);case 198:return s=this.buffer.readUInt32BE(this.offset),this.offset+=4,this.bin(s);case 199:if(s=this.buffer.readUInt8(this.offset),r=this.buffer.readInt8(this.offset+1),this.offset+=2,0===r)return this.arraybuffer(s);if(-1===r){const t=this.buffer.readUInt32BE(this.offset);return f=this.buffer.readInt32BE(this.offset+4),n=this.buffer.readUInt32BE(this.offset+8),this.offset+=12,new Date(1e3*(4294967296*f+n)+t/1e6)}return[r,this.bin(s)];case 200:return s=this.buffer.readUInt16BE(this.offset),r=this.buffer.readInt8(this.offset+2),this.offset+=3,0===r?this.arraybuffer(s):[r,this.bin(s)];case 201:return s=this.buffer.readUInt32BE(this.offset),r=this.buffer.readInt8(this.offset+4),this.offset+=5,0===r?this.arraybuffer(s):[r,this.bin(s)];case 202:return e=this.buffer.readFloatBE(this.offset),this.offset+=4,e;case 203:return e=this.buffer.readDoubleBE(this.offset),this.offset+=8,e;case 204:return e=this.buffer.readUInt8(this.offset),this.offset+=1,e;case 205:return e=this.buffer.readUInt16BE(this.offset),this.offset+=2,e;case 206:return e=this.buffer.readUInt32BE(this.offset),this.offset+=4,e;case 207:return f=this.buffer.readUInt32BE(this.offset)*Math.pow(2,32),n=this.buffer.readUInt32BE(this.offset+4),this.offset+=8,f+n;case 208:return e=this.buffer.readInt8(this.offset),this.offset+=1,e;case 209:return e=this.buffer.readInt16BE(this.offset),this.offset+=2,e;case 210:return e=this.buffer.readInt32BE(this.offset),this.offset+=4,e;case 211:return f=this.buffer.readInt32BE(this.offset)*Math.pow(2,32),n=this.buffer.readUInt32BE(this.offset+4),this.offset+=8,f+n;case 212:return r=this.buffer.readInt8(this.offset),this.offset+=1,0===r?void(this.offset+=1):[r,this.bin(1)];case 213:return r=this.buffer.readInt8(this.offset),this.offset+=1,[r,this.bin(2)];case 214:return r=this.buffer.readInt8(this.offset),this.offset+=1,-1===r?(e=this.buffer.readUInt32BE(this.offset),this.offset+=4,new Date(1e3*e)):[r,this.bin(4)];case 215:if(r=this.buffer.readInt8(this.offset),this.offset+=1,0===r)return f=this.buffer.readInt32BE(this.offset)*Math.pow(2,32),n=this.buffer.readUInt32BE(this.offset+4),this.offset+=8,new Date(f+n);if(-1===r){f=this.buffer.readUInt32BE(this.offset),n=this.buffer.readUInt32BE(this.offset+4),this.offset+=8;return new Date(1e3*(4294967296*(3&f)+n)+(f>>>2)/1e6)}return[r,this.bin(8)];case 216:return r=this.buffer.readInt8(this.offset),this.offset+=1,[r,this.bin(16)];case 217:return s=this.buffer.readUInt8(this.offset),this.offset+=1,this.str(s);case 218:return s=this.buffer.readUInt16BE(this.offset),this.offset+=2,this.str(s);case 219:return s=this.buffer.readUInt32BE(this.offset),this.offset+=4,this.str(s);case 220:return s=this.buffer.readUInt16BE(this.offset),this.offset+=2,this.array(s);case 221:return s=this.buffer.readUInt32BE(this.offset),this.offset+=4,this.array(s);case 222:return s=this.buffer.readUInt16BE(this.offset),this.offset+=2,this.map(s);case 223:return s=this.buffer.readUInt32BE(this.offset),this.offset+=4,this.map(s)}throw new Error("Could not parse")},l=function(t){const s=new e(t),r=s.parse();if(s.offset!==t.length)throw new Error(t.length-s.offset+" trailing bytes");return r}}var I=(d||(d=1,b.encode=function(){if(a)return h;function t(t,e,s){let r=0;for(let f=0,n=s.length;f<n;f++)r=s.charCodeAt(f),r<128?t[e++]=r:r<2048?(t[e++]=192|r>>6,t[e++]=128|63&r):r<55296||r>=57344?(t[e++]=224|r>>12,t[e++]=128|r>>6&63,t[e++]=128|63&r):(f++,r=65536+((1023&r)<<10|1023&s.charCodeAt(f)),t[e++]=240|r>>18,t[e++]=128|r>>12&63,t[e++]=128|r>>6&63,t[e++]=128|63&r)}a=1;const e=new Map,s=process.env.NOTEPACK_ENCODE_CACHE_MAX_SIZE||1024;function r(r,n,i){if(e.has(i)){const t=e.get(i);return n.push({bin:t,length:t.length,offset:r.length}),t.length}if(e.size>s)return f(r,n,i);const o=[],h=f(o,[],i),a=Buffer.allocUnsafe(h);for(let t=0,e=o.length;t<e;t++)a[t]=o[t];return t(a,o.length,i),n.push({bin:a,length:h,offset:r.length}),e.set(i,a),h}function f(t,e,s){let n=0,i=0,o=0,h=0;switch(typeof s){case"string":if(o=s.length>32?Buffer.byteLength(s):function(t){let e=0,s=0;for(let r=0,f=t.length;r<f;r++)e=t.charCodeAt(r),e<128?s+=1:e<2048?s+=2:e<55296||e>=57344?s+=3:(r++,s+=4);return s}(s),o<32)t.push(160|o),h=1;else if(o<256)t.push(217,o),h=2;else if(o<65536)t.push(218,o>>8,o),h=3;else{if(!(o<4294967296))throw new Error("String too long");t.push(219,o>>24,o>>16,o>>8,o),h=5}return e.push({str:s,length:o,offset:t.length}),h+o;case"number":return Math.floor(s)===s&&isFinite(s)?s>=0?s<128?(t.push(s),1):s<256?(t.push(204,s),2):s<65536?(t.push(205,s>>8,s),3):s<4294967296?(t.push(206,s>>24,s>>16,s>>8,s),5):(n=s/Math.pow(2,32)|0,i=s>>>0,t.push(207,n>>24,n>>16,n>>8,n,i>>24,i>>16,i>>8,i),9):s>=-32?(t.push(s),1):s>=-128?(t.push(208,s),2):s>=-32768?(t.push(209,s>>8,s),3):s>=-2147483648?(t.push(210,s>>24,s>>16,s>>8,s),5):(n=Math.floor(s/Math.pow(2,32)),i=s>>>0,t.push(211,n>>24,n>>16,n>>8,n,i>>24,i>>16,i>>8,i),9):(t.push(203),e.push({float:s,length:8,offset:t.length}),9);case"object":if(null===s)return t.push(192),1;if(Array.isArray(s)){if(o=s.length,o<16)t.push(144|o),h=1;else if(o<65536)t.push(220,o>>8,o),h=3;else{if(!(o<4294967296))throw new Error("Array too large");t.push(221,o>>24,o>>16,o>>8,o),h=5}for(let r=0;r<o;r++)h+=f(t,e,s[r]);return h}if(s instanceof Date){const e=s.getTime(),r=Math.floor(e/1e3),f=1e6*(e-1e3*r);return r>=0&&f>=0&&r<=17179869183?0===f&&r<=4294967295?(t.push(214,255,r>>24,r>>16,r>>8,r),6):(n=r/4294967296,i=4294967295&r,t.push(215,255,f>>22,f>>14,f>>6,n,i>>24,i>>16,i>>8,i),10):(n=Math.floor(r/4294967296),i=r>>>0,t.push(199,12,255,f>>24,f>>16,f>>8,f,n>>24,n>>16,n>>8,n,i>>24,i>>16,i>>8,i),15)}if(s instanceof Buffer){if(o=s.length,o<256)t.push(196,o),h=2;else if(o<65536)t.push(197,o>>8,o),h=3;else{if(!(o<4294967296))throw new Error("Buffer too large");t.push(198,o>>24,o>>16,o>>8,o),h=5}return e.push({bin:s,length:o,offset:t.length}),h+o}if(s instanceof ArrayBuffer||ArrayBuffer.isView(s))return f(t,e,Buffer.from(s));if("function"==typeof s.toJSON)return f(t,e,s.toJSON());const a=[],u=Object.keys(s);let c="";for(let t=0,e=u.length;t<e;t++)c=u[t],void 0!==s[c]&&"function"!=typeof s[c]&&a.push(c);if(o=a.length,o<16)t.push(128|o),h=1;else if(o<65536)t.push(222,o>>8,o),h=3;else{if(!(o<4294967296))throw new Error("Object too large");t.push(223,o>>24,o>>16,o>>8,o),h=5}for(let n=0;n<o;n++)c=a[n],h+=r(t,e,c),h+=f(t,e,s[c]);return h;case"boolean":return t.push(s?195:194),1;case"undefined":return t.push(192),1;default:if("function"==typeof s.toJSON)return f(t,e,s.toJSON());throw new Error("Could not encode")}}return h=function(e){const s=[],r=[],n=f(s,r,e),i=Buffer.allocUnsafe(n);let o=0,h=0,a=-1;r.length>0&&(a=r[0].offset);let u,c=0,l=0;for(let e=0,f=s.length;e<f;e++)for(i[h+e]=s[e];e+1===a;){if(u=r[o],c=u.length,l=h+a,u.bin)if(c>32)u.bin.copy(i,l,0,c);else{const t=u.bin;for(let e=0;e<c;e++)i[l+e]=t[e]}else u.str?c>32?i.write(u.str,l,c,"utf8"):t(i,l,u.str):void 0!==u.float&&i.writeDoubleBE(u.float,l);if(o++,h+=c,!r[o])break;a=r[o].offset}return i}}(),b.decode=E()),b);const g={_:{}};class y extends Error{constructor(t,e="WILCOCRYPT_ERROR"){super(t),this.name="WilcoCryptError",this.code=e,Error.captureStackTrace&&Error.captureStackTrace(this,y)}}g._.WilcoCryptError=y,g._.VERSION="2.1.0",g._.MIN_PASSWORD_LENGTH=6,g._.assertKeyAndIv=function(t,e){if(!Buffer.isBuffer(t)||32!==t.length)throw new y("Invalid encryption key (expected 32-byte Buffer)","INVALID_KEY");if(!Buffer.isBuffer(e)||12!==e.length)throw new y("Invalid IV (expected 12-byte Buffer for GCM)","INVALID_IV")},g._.assertPassword=function(t){if("string"!=typeof t||t.length<g._.MIN_PASSWORD_LENGTH)throw new y(`Password must be at least ${g._.MIN_PASSWORD_LENGTH} characters`,"WEAK_PASSWORD")},g._.constantTimeEqual=function(t,e){if(t.length!==e.length)return!1;let s=0;for(let r=0;r<t.length;r++)s|=t[r]^e[r];return 0===s},g._.encryptData=function(e,s,r){g._.assertKeyAndIv(s,r);const f=t("aes-256-gcm",s,r);return{ciphertext:Buffer.concat([f.update(e),f.final()]),authTag:f.getAuthTag()}},g._.decryptData=function(t,s,r,f){g._.assertKeyAndIv(r,f);try{const n=e("aes-256-gcm",r,f);return n.setAuthTag(Buffer.from(s,"hex")),Buffer.concat([n.update(Buffer.from(t,"hex")),n.final()])}catch{throw new y("Decryption failed (invalid password, corrupted data, or tampered file)","DECRYPTION_FAILED")}},g.encryptData=function(t,e,n=!0){let i;g._.assertPassword(e),i=n?f(t):t;const o=s(12),h=s(16),a=r(e,h,32),{ciphertext:u,authTag:c}=g._.encryptData(i,a,o),l={payload:u.toString("hex"),authTag:c.toString("hex"),salt:h.toString("hex"),iv:o.toString("hex"),version:g._.VERSION};return I.encode(l)},g.decryptData=function(t,e,s=!0){let f;g._.assertPassword(e);try{f=I.decode(t)}catch{throw new y("Invalid encrypted data format (not MessagePack)","INVALID_FORMAT")}if(f.version!==g._.VERSION)throw new y(`Version mismatch (expected ${g._.VERSION}, got ${f.version})`,"VERSION_MISMATCH");const i=r(e,Buffer.from(f.salt,"hex"),32),o=g._.decryptData(f.payload,f.authTag,i,Buffer.from(f.iv,"hex"));try{return s?n(o):o}catch{throw new y("Decryption succeeded but decompression failed (data may be corrupted or not compressed)","DECOMPRESSION_FAILED")}},g.encryptFile=function(t,e,s=!0){const r=i(t),f=g.encryptData(r,e,s);o(`${t}.enc`,f)},g.decryptFile=function(t,e,s=!0){if(!t.endsWith(".enc"))throw new y("Invalid file extension (expected .enc)","INVALID_FILE_EXTENSION");const r=i(t);return g.decryptData(r,e,s)};export{g as default};
package/rollup.config.js DELETED
@@ -1,54 +0,0 @@
1
- import { defineConfig } from 'rollup';
2
- import { nodeResolve } from '@rollup/plugin-node-resolve';
3
- import commonjs from '@rollup/plugin-commonjs';
4
- import replace from '@rollup/plugin-replace';
5
- import terser from '@rollup/plugin-terser';
6
- import { builtinModules } from 'module';
7
-
8
- export default defineConfig([
9
- {
10
- input: {
11
- wilcocrypt: 'src/wilcocrypt.js',
12
- cli: 'src/cli.js'
13
- },
14
- output: [
15
- {
16
- dir: 'dist',
17
- format: 'es',
18
- entryFileNames: '[name].js',
19
- banner: (chunk) => chunk.name === 'cli' ? '#!/usr/bin/env node' : null
20
- },
21
- {
22
- dir: 'dist',
23
- format: 'es',
24
- entryFileNames: '[name].min.js',
25
- banner: (chunk) => chunk.name === 'cli' ? '#!/usr/bin/env node' : null,
26
- plugins: [terser()]
27
- }
28
- ],
29
- external: [
30
- ...builtinModules,
31
- ...builtinModules.map(m => `node:${m}`)
32
- ],
33
- plugins: [
34
- nodeResolve({ preferBuiltins: true }),
35
- commonjs(),
36
- replace({ preventAssignment: true, 'process.env.NODE_ENV': JSON.stringify('production') })
37
- ]
38
- },
39
-
40
- {
41
- input: 'src/cli.js',
42
- output: {
43
- file: 'dist/cli.min.cjs',
44
- format: 'cjs',
45
- banner: '#!/usr/bin/env node'
46
- },
47
- plugins: [
48
- nodeResolve({ preferBuiltins: true }),
49
- commonjs(),
50
- replace({ preventAssignment: true, 'process.env.NODE_ENV': JSON.stringify('production') }),
51
- terser()
52
- ]
53
- }
54
- ]);
@@ -1,5 +0,0 @@
1
- {
2
- "main": "./dist/cli.min.cjs",
3
- "output": "./sea/wilcocrypt.sea",
4
- "disableExperimentalSEAWarning": true
5
- }