@pixelfactory/privatebin
Version:
a PrivateBin client written in typescript
3 lines (2 loc) • 2.32 kB
JavaScript
import t from"pako";import e from"axios";import a from"isomorphic-webcrypto";import{bytesToBase64 as s,base64ToBytes as n}from"byte-base64";class r{constructor(t){this.axios=e.create(t)}get(t,e){return this.axios.get(t,e)}post(t,e,a){return this.axios.post(t,e,a)}success(t){return t.data}}function i(t){return a.subtle.importKey("raw",t,"PBKDF2",!1,["deriveBits","deriveKey"])}function o(t,e,s,n){return a.subtle.deriveKey({name:"PBKDF2",salt:e,iterations:s,hash:"SHA-256"},t,{name:"AES-GCM",length:n},!1,["encrypt","decrypt"])}function c(t){return(new TextEncoder).encode(t)}async function u(e,n,r){const{burnafterreading:u,opendiscussion:p,compression:d,textformat:m}=r,f={algo:"aes",mode:"gcm",ks:256,ts:128,iter:1e5,textformat:m,compression:d,burnafterreading:u,opendiscussion:p};let g=c(JSON.stringify({paste:e}));return"zlib"===d&&(g=t.deflateRaw(g)),async function(t,e,n){const r=await i(e),u=a.getRandomValues(new Uint8Array(16)),p=a.getRandomValues(new Uint8Array(8)),d=await o(r,p,n.iter,n.ks),m=[[s(u),s(p),n.iter,n.ks,n.ts,n.algo,n.mode,n.compression],n.textformat,n.opendiscussion,n.burnafterreading],f=await a.subtle.encrypt({name:"AES-GCM",iv:u,additionalData:c(JSON.stringify(m)),tagLength:n.ts},d,t);return{ct:s(new Uint8Array(f)),adata:m}}(g,n,f)}async function p(e,s,r){const u=await async function(t,e,s){const r=n(t),u=s[0],p=n(u[0]),d=n(u[1]),m=u[2],f=u[4],g=await i(e),y=await o(g,d,m,256),h=await a.subtle.decrypt({name:"AES-GCM",iv:p,additionalData:c(JSON.stringify(s)),tagLength:f},y,r);return new Uint8Array(h)}(e,s,r);return"zlib"===r[0][7]?JSON.parse(t.inflateRaw(u,{to:"string"})):JSON.parse(function(t){return(new TextDecoder).decode(t)}(u))}class d extends r{constructor(t="https://privatebin.net"){super({baseURL:t,headers:{"Content-Type":"application/json","X-Requested-With":"JSONHttpRequest"}})}async sendText(t,e,a){const s=await u(t,e,a);return this.postPaste(s,a)}async getText(t,e){const{status:a,message:s,ct:n,adata:r}=await this.getPaste(t);if(0==a)return p(n,e,r);throw new Error(s)}getPaste(t){return this.get(`/?pasteid=${t}`).then(this.success)}postPaste(t,e){const{expire:a}=e,{ct:s,adata:n}=t;return this.post("/",{v:2,ct:s,adata:n,meta:{expire:a}}).then(this.success)}}export{d as PrivatebinClient,p as decryptText,u as encryptText};
//# sourceMappingURL=index.min.mjs.map