mirror of
https://github.com/moparisthebest/mail
synced 2024-11-26 02:42:17 -05:00
created portable version of util
This commit is contained in:
parent
e1c8866afe
commit
104bc734f6
@ -1,7 +1,9 @@
|
|||||||
|
var crypt = crypt;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Various utitity methods for crypto, encoding & decoding
|
* Various utitity methods for crypto, encoding & decoding
|
||||||
*/
|
*/
|
||||||
app.crypto.Util = function(window, uuid) {
|
var Util = function(window, uuid) {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -19,10 +21,15 @@ app.crypto.Util = function(window, uuid) {
|
|||||||
this.random = function(keySize) {
|
this.random = function(keySize) {
|
||||||
var keyBase64, keyBuf;
|
var keyBase64, keyBuf;
|
||||||
|
|
||||||
if (window.crypto && window.crypto.getRandomValues) {
|
if (typeof module !== 'undefined' && module.exports) {
|
||||||
|
keyBuf = crypt.randomBytes(keySize / 8);
|
||||||
|
keyBase64 = new Buffer(keyBuf).toString('base64');
|
||||||
|
|
||||||
|
} else if (window.crypto && window.crypto.getRandomValues) {
|
||||||
keyBuf = new Uint8Array(keySize / 8);
|
keyBuf = new Uint8Array(keySize / 8);
|
||||||
window.crypto.getRandomValues(keyBuf);
|
window.crypto.getRandomValues(keyBuf);
|
||||||
keyBase64 = window.btoa(this.uint8Arr2BinStr(keyBuf));
|
keyBase64 = window.btoa(this.uint8Arr2BinStr(keyBuf));
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// add an additional peace of entropy to the pot and stir with the sjcl prng
|
// add an additional peace of entropy to the pot and stir with the sjcl prng
|
||||||
sjcl.random.addEntropy((new Date()).valueOf(), 2, "calltime");
|
sjcl.random.addEntropy((new Date()).valueOf(), 2, "calltime");
|
||||||
@ -165,4 +172,33 @@ app.crypto.Util = function(window, uuid) {
|
|||||||
return str;
|
return str;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a str to base64 in a browser and in node.js
|
||||||
|
*/
|
||||||
|
this.str2Base64 = function(str) {
|
||||||
|
if (typeof module !== 'undefined' && module.exports) {
|
||||||
|
return new Buffer(str, 'binary').toString('base64');
|
||||||
|
} else {
|
||||||
|
return window.btoa(str);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a base64 encoded string in a browser and in node.js
|
||||||
|
*/
|
||||||
|
this.base642Str = function(str) {
|
||||||
|
if (typeof module !== 'undefined' && module.exports) {
|
||||||
|
return new Buffer(str, 'base64').toString('binary');
|
||||||
|
} else {
|
||||||
|
return window.atob(str);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
if (typeof module !== 'undefined' && module.exports) {
|
||||||
|
crypt = require('crypto');
|
||||||
|
module.exports = Util;
|
||||||
|
} else {
|
||||||
|
app.crypto.Util = Util;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user