1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-07-05 11:17:36 +00:00
seahub/media/js/repo_crypto.js

76 lines
39 KiB
JavaScript
Raw Normal View History

/* uuid.js | Copyright (c) 2010-2012 Robert Kieffer | MIT License - http://opensource.org/licenses/mit-license.php */
(function(){var _global=this;var _rng;if(typeof(require)=="function"){try{var _rb=require("crypto").randomBytes;_rng=_rb&&function(){return _rb(16);};}catch(e){}}if(!_rng&&_global.crypto&&crypto.getRandomValues){var _rnds8=new Uint8Array(16);_rng=function whatwgRNG(){crypto.getRandomValues(_rnds8);return _rnds8;};}if(!_rng){var _rnds=new Array(16);_rng=function(){for(var i=0,r;i<16;i++){if((i&3)===0){r=Math.random()*4294967296;}_rnds[i]=r>>>((i&3)<<3)&255;}return _rnds;};}var BufferClass=typeof(Buffer)=="function"?Buffer:Array;var _byteToHex=[];var _hexToByte={};for(var i=0;i<256;i++){_byteToHex[i]=(i+256).toString(16).substr(1);_hexToByte[_byteToHex[i]]=i;}function parse(s,buf,offset){var i=(buf&&offset)||0,ii=0;buf=buf||[];s.toLowerCase().replace(/[0-9a-f]{2}/g,function(oct){if(ii<16){buf[i+ii++]=_hexToByte[oct];}});while(ii<16){buf[i+ii++]=0;}return buf;}function unparse(buf,offset){var i=offset||0,bth=_byteToHex;return bth[buf[i++]]+bth[buf[i++]]+bth[buf[i++]]+bth[buf[i++]]+"-"+bth[buf[i++]]+bth[buf[i++]]+"-"+bth[buf[i++]]+bth[buf[i++]]+"-"+bth[buf[i++]]+bth[buf[i++]]+"-"+bth[buf[i++]]+bth[buf[i++]]+bth[buf[i++]]+bth[buf[i++]]+bth[buf[i++]]+bth[buf[i++]];}var _seedBytes=_rng();var _nodeId=[_seedBytes[0]|1,_seedBytes[1],_seedBytes[2],_seedBytes[3],_seedBytes[4],_seedBytes[5]];var _clockseq=(_seedBytes[6]<<8|_seedBytes[7])&16383;var _lastMSecs=0,_lastNSecs=0;function v1(options,buf,offset){var i=buf&&offset||0;var b=buf||[];options=options||{};var clockseq=options.clockseq!=null?options.clockseq:_clockseq;var msecs=options.msecs!=null?options.msecs:new Date().getTime();var nsecs=options.nsecs!=null?options.nsecs:_lastNSecs+1;var dt=(msecs-_lastMSecs)+(nsecs-_lastNSecs)/10000;if(dt<0&&options.clockseq==null){clockseq=clockseq+1&16383;}if((dt<0||msecs>_lastMSecs)&&options.nsecs==null){nsecs=0;}if(nsecs>=10000){throw new Error("uuid.v1(): Can't create more than 10M uuids/sec");}_lastMSecs=msecs;_lastNSecs=nsecs;_clockseq=clockseq;msecs+=12219292800000;var tl=((msecs&268435455)*10000+nsecs)%4294967296;b[i++]=tl>>>24&255;b[i++]=tl>>>16&255;b[i++]=tl>>>8&255;b[i++]=tl&255;var tmh=(msecs/4294967296*10000)&268435455;b[i++]=tmh>>>8&255;b[i++]=tmh&255;b[i++]=tmh>>>24&15|16;b[i++]=tmh>>>16&255;b[i++]=clockseq>>>8|128;b[i++]=clockseq&255;var node=options.node||_nodeId;for(var n=0;n<6;n++){b[i+n]=node[n];}return buf?buf:unparse(b);}function v4(options,buf,offset){var i=buf&&offset||0;if(typeof(options)=="string"){buf=options=="binary"?new BufferClass(16):null;options=null;}options=options||{};var rnds=options.random||(options.rng||_rng)();rnds[6]=(rnds[6]&15)|64;rnds[8]=(rnds[8]&63)|128;if(buf){for(var ii=0;ii<16;ii++){buf[i+ii]=rnds[ii];}}return buf||unparse(rnds);}var uuid=v4;uuid.v1=v1;uuid.v4=v4;uuid.parse=parse;uuid.unparse=unparse;uuid.BufferClass=BufferClass;if(typeof define==="function"&&define.amd){define(function(){return uuid;});}else{if(typeof(module)!="undefined"&&module.exports){module.exports=uuid;}else{var _previousRoot=_global.uuid;uuid.noConflict=function(){_global.uuid=_previousRoot;return uuid;};_global.uuid=uuid;}}}).call(this);
/* sjcl.js */
"use strict";function q(a){throw a;}var t=void 0,u=!1;var sjcl={cipher:{},hash:{},keyexchange:{},mode:{},misc:{},codec:{},exception:{corrupt:function(a){this.toString=function(){return"CORRUPT: "+this.message;};this.message=a;},invalid:function(a){this.toString=function(){return"INVALID: "+this.message;};this.message=a;},bug:function(a){this.toString=function(){return"BUG: "+this.message;};this.message=a;},notReady:function(a){this.toString=function(){return"NOT READY: "+this.message;};this.message=a;}}};"undefined"!=typeof module&&module.exports&&(module.exports=sjcl);sjcl.cipher.aes=function(a){this.j[0][0][0]||this.D();var b,c,d,e,f=this.j[0][4],g=this.j[1];b=a.length;var h=1;4!==b&&(6!==b&&8!==b)&&q(new sjcl.exception.invalid("invalid aes key size"));this.a=[d=a.slice(0),e=[]];for(a=b;a<4*b+28;a++){c=d[a-1];if(0===a%b||8===b&&4===a%b){c=f[c>>>24]<<24^f[c>>16&255]<<16^f[c>>8&255]<<8^f[c&255],0===a%b&&(c=c<<8^c>>>24^h<<24,h=h<<1^283*(h>>7));}d[a]=d[a-b]^c;}for(b=0;a;b++,a--){c=d[b&3?a:a-4],e[b]=4>=a||4>b?c:g[0][f[c>>>24]]^g[1][f[c>>16&255]]^g[2][f[c>>8&255]]^g[3][f[c&255]];}};sjcl.cipher.aes.prototype={encrypt:function(a){return y(this,a,0);},decrypt:function(a){return y(this,a,1);},j:[[[],[],[],[],[]],[[],[],[],[],[]]],D:function(){var a=this.j[0],b=this.j[1],c=a[4],d=b[4],e,f,g,h=[],l=[],k,n,m,p;for(e=0;256>e;e++){l[(h[e]=e<<1^283*(e>>7))^e]=e;}for(f=g=0;!c[f];f^=k||1,g=l[g]||1){m=g^g<<1^g<<2^g<<3^g<<4;m=m>>8^m&255^99;c[f]=m;d[m]=f;n=h[e=h[k=h[f]]];p=16843009*n^65537*e^257*k^16843008*f;n=257*h[m]^16843008*m;for(e=0;4>e;e++){a[e][f]=n=n<<24^n>>>8,b[e][m]=p=p<<24^p>>>8;}}for(e=0;5>e;e++){a[e]=a[e].slice(0),b[e]=b[e].slice(0);}}};function y(a,b,c){4!==b.length&&q(new sjcl.exception.invalid("invalid aes block size"));var d=a.a[c],e=b[0]^d[0],f=b[c?3:1]^d[1],g=b[2]^d[2];b=b[c?1:3]^d[3];var h,l,k,n=d.length/4-2,m,p=4,s=[0,0,0,0];h=a.j[c];a=h[0];var r=h[1],v=h[2],w=h[3],x=h[4];for(m=0;m<n;m++){h=a[e>>>24]^r[f>>16&255]^v[g>>8&255]^w[b&255]^d[p],l=a[f>>>24]^r[g>>16&255]^v[b>>8&255]^w[e&255]^d[p+1],k=a[g>>>24]^r[b>>16&255]^v[e>>8&255]^w[f&255]^d[p+2],b=a[b>>>24]^r[e>>16&255]^v[f>>8&255]^w[g&255]^d[p+3],p+=4,e=h,f=l,g=k;}for(m=0;4>m;m++){s[c?3&-m:m]=x[e>>>24]<<24^x[f>>16&255]<<16^x[g>>8&255]<<8^x[b&255]^d[p++],h=e,e=f,f=g,g=b,b=h;}return s;}sjcl.bitArray={bitSlice:function(a,b,c){a=sjcl.bitArray.O(a.slice(b/32),32-(b&31)).slice(1);return c===t?a:sjcl.bitArray.clamp(a,c-b);},extract:function(a,b,c){var d=Math.floor(-b-c&31);return((b+c-1^b)&-32?a[b/32|0]<<32-d^a[b/32+1|0]>>>d:a[b/32|0]>>>d)&(1<<c)-1;},concat:function(a,b){if(0===a.length||0===b.length){return a.concat(b);}var c=a[a.length-1],d=sjcl.bitArray.getPartial(c);return 32===d?a.concat(b):sjcl.bitArray.O(b,d,c|0,a.slice(0,a.length-1));},bitLength:function(a){var b=a.length;return 0===b?0:32*(b-1)+sjcl.bitArray.getPartial(a[b-1]);},clamp:function(a,b){if(32*a.length<b){return a;}a=a.slice(0,Math.ceil(b/32));var c=a.length;b&=31;0<c&&b&&(a[c-1]=sjcl.bitArray.partial(b,a[c-1]&2147483648>>b-1,1));return a;},partial:function(a,b,c){return 32===a?b:(c?b|0:b<<32-a)+1099511627776*a;},getPartial:function(a){return Math.round(a/1099511627776)||32;},equal:function(a,b){if(sjcl.bitArray.bitLength(a)!==sjcl.bitArray.bitLength(b)){return u;}var c=0,d;for(d=0;d<a.length;d++){c|=a[d]^b[d];}return 0===c;},O:function(a,b,c,d){var e;e=0;for(d===t&&(d=[]);32<=b;b-=32){d.push(c),c=0;}if(0===b){return d.concat(a);}for(e=0;e<a.length;e++){d.push(c|a[e]>>>b),c=a[e]<<32-b;}e=a.length?a[a.length-1]:0;a=sjcl.bitArray.getPartial(e);d.push(sjcl.bitArray.partial(b+a&31,32<b+a?c:d.pop(),1));return d;},k:function(a,b){return[a[0]^b[0],a[1]^b[1],a[2]^b[2],a[3]^b[3]];}};sjcl.codec.utf8String={fromBits:function(a){var b="",c=sjcl.bitArray.bitLength(a),d,e;for(d=0;d<c/8;d++){0===(d&3)&&(e=a[d/4]),b+=String.fromCharCode(e>>>24),e<<=8;}return decodeURIComponent(escape(b));},toBits:function(a){a=unescape(encodeURIComponent(a));var b=[],c,d=0;for(c=0;c<a.length;c++){d=d<<8|a.charCodeAt(c),3===(c&3)&&(b.push(d),d=0);}c&3&&b.push(sjcl.bitArray.partial(8*(c&3),d));return b;}};sjcl.codec.hex={fromBits:function(a)
/* aes.js */
/*
* CryptoJS v3.1.2
* code.google.com/p/crypto-js
* (c) 2009-2013 by Jeff Mott. All rights reserved.
* code.google.com/p/crypto-js/wiki/License
* */
var CryptoJS=CryptoJS||function(u,p){var d={},l=d.lib={},s=function(){},t=l.Base={extend:function(a){s.prototype=this;var c=new s;a&&c.mixIn(a);c.hasOwnProperty("init")||(c.init=function(){c.$super.init.apply(this,arguments);});c.init.prototype=c;c.$super=this;return c;},create:function(){var a=this.extend();a.init.apply(a,arguments);return a;},init:function(){},mixIn:function(a){for(var c in a){a.hasOwnProperty(c)&&(this[c]=a[c]);}a.hasOwnProperty("toString")&&(this.toString=a.toString);},clone:function(){return this.init.prototype.extend(this);}},r=l.WordArray=t.extend({init:function(a,c){a=this.words=a||[];this.sigBytes=c!=p?c:4*a.length;},toString:function(a){return(a||v).stringify(this);},concat:function(a){var c=this.words,e=a.words,j=this.sigBytes;a=a.sigBytes;this.clamp();if(j%4){for(var k=0;k<a;k++){c[j+k>>>2]|=(e[k>>>2]>>>24-8*(k%4)&255)<<24-8*((j+k)%4);}}else{if(65535<e.length){for(k=0;k<a;k+=4){c[j+k>>>2]=e[k>>>2];}}else{c.push.apply(c,e);}}this.sigBytes+=a;return this;},clamp:function(){var a=this.words,c=this.sigBytes;a[c>>>2]&=4294967295<<32-8*(c%4);a.length=u.ceil(c/4);},clone:function(){var a=t.clone.call(this);a.words=this.words.slice(0);return a;},random:function(a){for(var c=[],e=0;e<a;e+=4){c.push(4294967296*u.random()|0);}return new r.init(c,a);}}),w=d.enc={},v=w.Hex={stringify:function(a){var c=a.words;a=a.sigBytes;for(var e=[],j=0;j<a;j++){var k=c[j>>>2]>>>24-8*(j%4)&255;e.push((k>>>4).toString(16));e.push((k&15).toString(16));}return e.join("");},parse:function(a){for(var c=a.length,e=[],j=0;j<c;j+=2){e[j>>>3]|=parseInt(a.substr(j,2),16)<<24-4*(j%8);}return new r.init(e,c/2);}},b=w.Latin1={stringify:function(a){var c=a.words;a=a.sigBytes;for(var e=[],j=0;j<a;j++){e.push(String.fromCharCode(c[j>>>2]>>>24-8*(j%4)&255));}return e.join("");},parse:function(a){for(var c=a.length,e=[],j=0;j<c;j++){e[j>>>2]|=(a.charCodeAt(j)&255)<<24-8*(j%4);}return new r.init(e,c);}},x=w.Utf8={stringify:function(a){try{return decodeURIComponent(escape(b.stringify(a)));}catch(c){throw Error("Malformed UTF-8 data");}},parse:function(a){return b.parse(unescape(encodeURIComponent(a)));}},q=l.BufferedBlockAlgorithm=t.extend({reset:function(){this._data=new r.init;this._nDataBytes=0;},_append:function(a){"string"==typeof a&&(a=x.parse(a));this._data.concat(a);this._nDataBytes+=a.sigBytes;},_process:function(a){var c=this._data,e=c.words,j=c.sigBytes,k=this.blockSize,b=j/(4*k),b=a?u.ceil(b):u.max((b|0)-this._minBufferSize,0);a=b*k;j=u.min(4*a,j);if(a){for(var q=0;q<a;q+=k){this._doProcessBlock(e,q);}q=e.splice(0,a);c.sigBytes-=j;}return new r.init(q,j);},clone:function(){var a=t.clone.call(this);a._data=this._data.clone();return a;},_minBufferSize:0});l.Hasher=q.extend({cfg:t.extend(),init:function(a){this.cfg=this.cfg.extend(a);this.reset();},reset:function(){q.reset.call(this);this._doReset();},update:function(a){this._append(a);this._process();return this;},finalize:function(a){a&&this._append(a);return this._doFinalize();},blockSize:16,_createHelper:function(a){return function(b,e){return(new a.init(e)).finalize(b);};},_createHmacHelper:function(a){return function(b,e){return(new n.HMAC.init(a,e)).finalize(b);};}});var n=d.algo={};return d;}(Math);(function(){var u=CryptoJS,p=u.lib.WordArray;u.enc.Base64={stringify:function(d){var l=d.words,p=d.sigBytes,t=this._map;d.clamp();d=[];for(var r=0;r<p;r+=3){for(var w=(l[r>>>2]>>>24-8*(r%4)&255)<<16|(l[r+1>>>2]>>>24-8*((r+1)%4)&255)<<8|l[r+2>>>2]>>>24-8*((r+2)%4)&255,v=0;4>v&&r+0.75*v<p;v++){d.push(t.charAt(w>>>6*(3-v)&63));}}if(l=t.charAt(64)){for(;d.length%4;){d.push(l);}}return d.join("");},parse:function(d){var l=d.length,s=this._map,t=s.charAt(64);t&&(t=d.indexOf(t),-1!=t&&(l=t));for(var t=[],r=0,w=0;w<l;w++){if(w%4){var v=s.indexOf(d.charAt(w-1))<<2*(w%4),b=s.indexOf(d.charAt(w))>>>6-2*(w%4);t[r>>>2]|=(v|b)<<24-8*(r%4);r++;}}return p.create(t,r);},_map:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="};})();(function(u){function p(b,n,a,c,e,j,k){b=b+(n&a|~n&c)+e+k;return(b<<j|b>>>32-j)+n;}function d(b,n,a,c,e,j,k){b=b+(n&c|a&~c)+e+k;return(b<<j|b>>>32-j)
/* client_crypto.js | by lj */
var salt = sjcl.codec.bytes.toBits([0xda, 0x90, 0x45, 0xc3, 0x06, 0xc7, 0xcc, 0x26]);
// gen magic_str
function gen_magic_str(repo_id, passwd) {
var magic_array = sjcl.misc.pbkdf2(repo_id + passwd, salt, 1000, 32*8, null);
var magic_str = sjcl.codec.hex.fromBits(magic_array); //convert to hex
return magic_str;
}
function FileKey(passwd) {
this.passwd = passwd;
}
FileKey.prototype.pre = function() {
var passwd = this.passwd;
var key_array = sjcl.misc.pbkdf2(passwd, salt, 1000, 32*8, null);
var iv_array = sjcl.misc.pbkdf2(key_array, salt, 10, 32*8, null);
var key = sjcl.codec.hex.fromBits(key_array);
var iv = sjcl.codec.hex.fromBits(iv_array);
return {key: key, iv:iv};
};
// generate an encrypted file key
FileKey.prototype.gen_enc = function() {
var file_key_array;
if (window.crypto && window.crypto.getRandomValues) {
file_key_array = new Uint32Array(8);
window.crypto.getRandomValues(file_key_array);
} else {
file_key_array = [];
for (var i = 0; i < 8; i++) {
file_key_array.push(parseInt(Math.random() * Math.pow(2,32)));
}
}
var pre = this.pre();
var key = pre.key, iv = pre.iv;
var encrypted_file_key_obj = CryptoJS.AES.encrypt(CryptoJS.lib.WordArray.create(file_key_array), CryptoJS.enc.Hex.parse(key), {iv: CryptoJS.enc.Hex.parse(iv)});
var encrypted_file_key = encrypted_file_key_obj.ciphertext.toString(CryptoJS.enc.Hex); // convert to hex
return encrypted_file_key;
};
FileKey.prototype.decrypt = function(encrypted_file_key) {
var pre = this.pre();
var key = pre.key, iv = pre.iv;
var file_key_array = CryptoJS.AES.decrypt(CryptoJS.enc.Hex.parse(encrypted_file_key).toString(CryptoJS.enc.Base64), CryptoJS.enc.Hex.parse(key), {iv: CryptoJS.enc.Hex.parse(iv)});
var file_key = file_key_array.toString(CryptoJS.enc.Hex);
return file_key;
};
// gen key, iv: used in file encrypt/decrypt
function gen_enc_key_iv(file_key) {
var enc_key_array = sjcl.misc.pbkdf2(sjcl.codec.hex.toBits(file_key), salt, 1000, 32*8, null);
var enc_iv_array = sjcl.misc.pbkdf2(enc_key_array, salt, 10, 32*8, null);
var enc_key = sjcl.codec.hex.fromBits(enc_key_array);
var enc_iv = sjcl.codec.hex.fromBits(enc_iv_array);
return {key: enc_key, iv:enc_iv};
}