Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ultraviolet
GitHub Repository: ultraviolet/bitaddress.org
Path: blob/master/src/cryptojs.js
248 views
1
/*!
2
* Crypto-JS v2.5.4 Crypto.js
3
* http://code.google.com/p/crypto-js/
4
* Copyright (c) 2009-2013, Jeff Mott. All rights reserved.
5
* http://code.google.com/p/crypto-js/wiki/License
6
*/
7
if (typeof Crypto == "undefined" || !Crypto.util) {
8
(function () {
9
10
var base64map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
11
12
// Global Crypto object
13
var Crypto = window.Crypto = {};
14
15
// Crypto utilities
16
var util = Crypto.util = {
17
18
// Bit-wise rotate left
19
rotl: function (n, b) {
20
return (n << b) | (n >>> (32 - b));
21
},
22
23
// Bit-wise rotate right
24
rotr: function (n, b) {
25
return (n << (32 - b)) | (n >>> b);
26
},
27
28
// Swap big-endian to little-endian and vice versa
29
endian: function (n) {
30
31
// If number given, swap endian
32
if (n.constructor == Number) {
33
return util.rotl(n, 8) & 0x00FF00FF |
34
util.rotl(n, 24) & 0xFF00FF00;
35
}
36
37
// Else, assume array and swap all items
38
for (var i = 0; i < n.length; i++)
39
n[i] = util.endian(n[i]);
40
return n;
41
42
},
43
44
// Generate an array of any length of random bytes
45
randomBytes: function (n) {
46
for (var bytes = []; n > 0; n--)
47
bytes.push(Math.floor(Math.random() * 256));
48
return bytes;
49
},
50
51
// Convert a byte array to big-endian 32-bit words
52
bytesToWords: function (bytes) {
53
for (var words = [], i = 0, b = 0; i < bytes.length; i++, b += 8)
54
words[b >>> 5] |= (bytes[i] & 0xFF) << (24 - b % 32);
55
return words;
56
},
57
58
// Convert big-endian 32-bit words to a byte array
59
wordsToBytes: function (words) {
60
for (var bytes = [], b = 0; b < words.length * 32; b += 8)
61
bytes.push((words[b >>> 5] >>> (24 - b % 32)) & 0xFF);
62
return bytes;
63
},
64
65
// Convert a byte array to a hex string
66
bytesToHex: function (bytes) {
67
for (var hex = [], i = 0; i < bytes.length; i++) {
68
hex.push((bytes[i] >>> 4).toString(16));
69
hex.push((bytes[i] & 0xF).toString(16));
70
}
71
return hex.join("");
72
},
73
74
// Convert a hex string to a byte array
75
hexToBytes: function (hex) {
76
for (var bytes = [], c = 0; c < hex.length; c += 2)
77
bytes.push(parseInt(hex.substr(c, 2), 16));
78
return bytes;
79
},
80
81
// Convert a byte array to a base-64 string
82
bytesToBase64: function (bytes) {
83
for (var base64 = [], i = 0; i < bytes.length; i += 3) {
84
var triplet = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2];
85
for (var j = 0; j < 4; j++) {
86
if (i * 8 + j * 6 <= bytes.length * 8)
87
base64.push(base64map.charAt((triplet >>> 6 * (3 - j)) & 0x3F));
88
else base64.push("=");
89
}
90
}
91
92
return base64.join("");
93
},
94
95
// Convert a base-64 string to a byte array
96
base64ToBytes: function (base64) {
97
// Remove non-base-64 characters
98
base64 = base64.replace(/[^A-Z0-9+\/]/ig, "");
99
100
for (var bytes = [], i = 0, imod4 = 0; i < base64.length; imod4 = ++i % 4) {
101
if (imod4 == 0) continue;
102
bytes.push(((base64map.indexOf(base64.charAt(i - 1)) & (Math.pow(2, -2 * imod4 + 8) - 1)) << (imod4 * 2)) |
103
(base64map.indexOf(base64.charAt(i)) >>> (6 - imod4 * 2)));
104
}
105
106
return bytes;
107
}
108
109
};
110
111
// Crypto character encodings
112
var charenc = Crypto.charenc = {};
113
114
// UTF-8 encoding
115
var UTF8 = charenc.UTF8 = {
116
117
// Convert a string to a byte array
118
stringToBytes: function (str) {
119
return Binary.stringToBytes(unescape(encodeURIComponent(str)));
120
},
121
122
// Convert a byte array to a string
123
bytesToString: function (bytes) {
124
return decodeURIComponent(escape(Binary.bytesToString(bytes)));
125
}
126
127
};
128
129
// Binary encoding
130
var Binary = charenc.Binary = {
131
132
// Convert a string to a byte array
133
stringToBytes: function (str) {
134
for (var bytes = [], i = 0; i < str.length; i++)
135
bytes.push(str.charCodeAt(i) & 0xFF);
136
return bytes;
137
},
138
139
// Convert a byte array to a string
140
bytesToString: function (bytes) {
141
for (var str = [], i = 0; i < bytes.length; i++)
142
str.push(String.fromCharCode(bytes[i]));
143
return str.join("");
144
}
145
146
};
147
148
})();
149
}
150