start to split into node commonjs style modules
- no longer is the global Bitcoin used for modules - cleaner and more maintainable code - add more tests
This commit is contained in:
parent
a6f05fb505
commit
0faac29134
35 changed files with 3401 additions and 3169 deletions
src
59
src/convert.js
Normal file
59
src/convert.js
Normal file
|
@ -0,0 +1,59 @@
|
|||
// convert to/from various values
|
||||
|
||||
var base64map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
||||
// Convert a byte array to a hex string
|
||||
module.exports.bytesToHex = function(bytes) {
|
||||
for (var hex = [], i = 0; i < bytes.length; i++) {
|
||||
hex.push((bytes[i] >>> 4).toString(16));
|
||||
hex.push((bytes[i] & 0xF).toString(16));
|
||||
}
|
||||
return hex.join("");
|
||||
};
|
||||
|
||||
// Convert a hex string to a byte array
|
||||
module.exports.hexToBytes = function(hex) {
|
||||
for (var bytes = [], c = 0; c < hex.length; c += 2)
|
||||
bytes.push(parseInt(hex.substr(c, 2), 16));
|
||||
return bytes;
|
||||
}
|
||||
|
||||
// Convert a byte array to a base-64 string
|
||||
module.exports.bytesToBase64 = function(bytes) {
|
||||
// Use browser-native function if it exists
|
||||
if (typeof btoa == "function") return btoa(Binary.bytesToString(bytes));
|
||||
|
||||
for(var base64 = [], i = 0; i < bytes.length; i += 3) {
|
||||
var triplet = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2];
|
||||
for (var j = 0; j < 4; j++) {
|
||||
if (i * 8 + j * 6 <= bytes.length * 8)
|
||||
base64.push(base64map.charAt((triplet >>> 6 * (3 - j)) & 0x3F));
|
||||
else base64.push("=");
|
||||
}
|
||||
}
|
||||
|
||||
return base64.join("");
|
||||
}
|
||||
|
||||
|
||||
// Convert a base-64 string to a byte array
|
||||
module.exports.base64ToBytes = function(base64) {
|
||||
// Use browser-native function if it exists
|
||||
if (typeof atob == "function") return Binary.stringToBytes(atob(base64));
|
||||
|
||||
// Remove non-base-64 characters
|
||||
base64 = base64.replace(/[^A-Z0-9+\/]/ig, "");
|
||||
|
||||
for (var bytes = [], i = 0, imod4 = 0; i < base64.length; imod4 = ++i % 4) {
|
||||
if (imod4 == 0) continue;
|
||||
bytes.push(((base64map.indexOf(base64.charAt(i - 1)) & (Math.pow(2, -2 * imod4 + 8) - 1)) << (imod4 * 2)) |
|
||||
(base64map.indexOf(base64.charAt(i)) >>> (6 - imod4 * 2)));
|
||||
}
|
||||
|
||||
return bytes;
|
||||
}
|
||||
|
||||
// utf8 and binary?
|
||||
//stringToBytes
|
||||
//bytesToString
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue