github.com/jincm/wesharechain@v0.0.0-20210122032815-1537409ce26a/app/lib/crypto1/crypto/crypto.js (about) 1 /*! 2 * Crypto-JS v1.1.0 3 * http://code.google.com/p/crypto-js/ 4 * Copyright (c) 2009, Jeff Mott. All rights reserved. 5 * http://code.google.com/p/crypto-js/wiki/License 6 */ 7 (function(){ 8 9 var base64map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 10 11 // Global Crypto object 12 window.Crypto = {}; 13 14 // Crypto utilities 15 var util = Crypto.util = { 16 17 // Bit-wise rotate left 18 rotl: function (n, b) { 19 return (n << b) | (n >>> (32 - b)); 20 }, 21 22 // Bit-wise rotate right 23 rotr: function (n, b) { 24 return (n << (32 - b)) | (n >>> b); 25 }, 26 27 // Swap big-endian to little-endian and vice versa 28 endian: function (n) { 29 30 // If number given, swap endian 31 if (n.constructor == Number) { 32 return util.rotl(n, 8) & 0x00FF00FF | 33 util.rotl(n, 24) & 0xFF00FF00; 34 } 35 36 // Else, assume array and swap all items 37 for (var i = 0; i < n.length; i++) 38 n[i] = util.endian(n[i]); 39 return n; 40 41 }, 42 43 // Generate an array of any length of random bytes 44 randomBytes: function (n) { 45 for (var bytes = []; n > 0; n--) 46 bytes.push(Math.floor(Math.random() * 256)); 47 return bytes; 48 }, 49 50 // Convert a string to a byte array 51 stringToBytes: function (str) { 52 var bytes = []; 53 for (var i = 0; i < str.length; i++) 54 bytes.push(str.charCodeAt(i)); 55 return bytes; 56 }, 57 58 // Convert a byte array to a string 59 bytesToString: function (bytes) { 60 var str = []; 61 for (var i = 0; i < bytes.length; i++) 62 str.push(String.fromCharCode(bytes[i])); 63 return str.join(""); 64 }, 65 66 // Convert a string to big-endian 32-bit words 67 stringToWords: function (str) { 68 var words = []; 69 for (var c = 0, b = 0; c < str.length; c++, b += 8) 70 words[b >>> 5] |= str.charCodeAt(c) << (24 - b % 32); 71 return words; 72 }, 73 74 // Convert a byte array to big-endian 32-bits words 75 bytesToWords: function (bytes) { 76 var words = []; 77 for (var i = 0, b = 0; i < bytes.length; i++, b += 8) 78 words[b >>> 5] |= bytes[i] << (24 - b % 32); 79 return words; 80 }, 81 82 // Convert big-endian 32-bit words to a byte array 83 wordsToBytes: function (words) { 84 var bytes = []; 85 for (var b = 0; b < words.length * 32; b += 8) 86 bytes.push((words[b >>> 5] >>> (24 - b % 32)) & 0xFF); 87 return bytes; 88 }, 89 90 // Convert a byte array to a hex string 91 bytesToHex: function (bytes) { 92 var hex = []; 93 for (var i = 0; i < bytes.length; i++) { 94 hex.push((bytes[i] >>> 4).toString(16)); 95 hex.push((bytes[i] & 0xF).toString(16)); 96 } 97 return hex.join(""); 98 }, 99 100 // Convert a hex string to a byte array 101 hexToBytes: function (hex) { 102 var bytes = []; 103 for (var c = 0; c < hex.length; c += 2) 104 bytes.push(parseInt(hex.substr(c, 2), 16)); 105 return bytes; 106 }, 107 108 // Convert a byte array to a base-64 string 109 bytesToBase64: function (bytes) { 110 111 // Use browser-native function if it exists 112 if (typeof btoa == "function") return btoa(util.bytesToString(bytes)); 113 114 var base64 = [], 115 overflow; 116 117 for (var i = 0; i < bytes.length; i++) { 118 switch (i % 3) { 119 case 0: 120 base64.push(base64map.charAt(bytes[i] >>> 2)); 121 overflow = (bytes[i] & 0x3) << 4; 122 break; 123 case 1: 124 base64.push(base64map.charAt(overflow | (bytes[i] >>> 4))); 125 overflow = (bytes[i] & 0xF) << 2; 126 break; 127 case 2: 128 base64.push(base64map.charAt(overflow | (bytes[i] >>> 6))); 129 base64.push(base64map.charAt(bytes[i] & 0x3F)); 130 overflow = -1; 131 } 132 } 133 134 // Encode overflow bits, if there are any 135 if (overflow != undefined && overflow != -1) 136 base64.push(base64map.charAt(overflow)); 137 138 // Add padding 139 while (base64.length % 4 != 0) base64.push("="); 140 141 return base64.join(""); 142 143 }, 144 145 // Convert a base-64 string to a byte array 146 base64ToBytes: function (base64) { 147 148 // Use browser-native function if it exists 149 if (typeof atob == "function") return util.stringToBytes(atob(base64)); 150 151 // Remove non-base-64 characters 152 base64 = base64.replace(/[^A-Z0-9+\/]/ig, ""); 153 154 var bytes = []; 155 156 for (var i = 0; i < base64.length; i++) { 157 switch (i % 4) { 158 case 1: 159 bytes.push((base64map.indexOf(base64.charAt(i - 1)) << 2) | 160 (base64map.indexOf(base64.charAt(i)) >>> 4)); 161 break; 162 case 2: 163 bytes.push(((base64map.indexOf(base64.charAt(i - 1)) & 0xF) << 4) | 164 (base64map.indexOf(base64.charAt(i)) >>> 2)); 165 break; 166 case 3: 167 bytes.push(((base64map.indexOf(base64.charAt(i - 1)) & 0x3) << 6) | 168 (base64map.indexOf(base64.charAt(i)))); 169 break; 170 } 171 } 172 173 return bytes; 174 175 } 176 177 }; 178 179 // Crypto mode namespace 180 Crypto.mode = {}; 181 182 })();