/ src / test / resources / Base58.js
Base58.js
  1  // Generated by CoffeeScript 1.8.0
  2  (function() {
  3    var ALPHABET, ALPHABET_MAP, Base58, i;
  4  
  5    Base58 = (typeof module !== "undefined" && module !== null ? module.exports : void 0) || (window.Base58 = {});
  6  
  7    ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
  8  
  9    ALPHABET_MAP = {};
 10  
 11    i = 0;
 12  
 13    while (i < ALPHABET.length) {
 14      ALPHABET_MAP[ALPHABET.charAt(i)] = i;
 15      i++;
 16    }
 17  
 18    Base58.encode = function(buffer) {
 19      var carry, digits, j;
 20      if (buffer.length === 0) {
 21        return "";
 22      }
 23      i = void 0;
 24      j = void 0;
 25      digits = [0];
 26      i = 0;
 27      while (i < buffer.length) {
 28        j = 0;
 29        while (j < digits.length) {
 30          digits[j] <<= 8;
 31          j++;
 32        }
 33        digits[0] += buffer[i];
 34        carry = 0;
 35        j = 0;
 36        while (j < digits.length) {
 37          digits[j] += carry;
 38          carry = (digits[j] / 58) | 0;
 39          digits[j] %= 58;
 40          ++j;
 41        }
 42        while (carry) {
 43          digits.push(carry % 58);
 44          carry = (carry / 58) | 0;
 45        }
 46        i++;
 47      }
 48      i = 0;
 49      while (buffer[i] === 0 && i < buffer.length - 1) {
 50        digits.push(0);
 51        i++;
 52      }
 53      return digits.reverse().map(function(digit) {
 54        return ALPHABET[digit];
 55      }).join("");
 56    };
 57  
 58    Base58.decode = function(string) {
 59      var bytes, c, carry, j;
 60      if (string.length === 0) {
 61        return new (typeof Uint8Array !== "undefined" && Uint8Array !== null ? Uint8Array : Buffer)(0);
 62      }
 63      i = void 0;
 64      j = void 0;
 65      bytes = [0];
 66      i = 0;
 67      while (i < string.length) {
 68        c = string[i];
 69        if (!(c in ALPHABET_MAP)) {
 70          throw "Base58.decode received unacceptable input. Character '" + c + "' is not in the Base58 alphabet.";
 71        }
 72        j = 0;
 73        while (j < bytes.length) {
 74          bytes[j] *= 58;
 75          j++;
 76        }
 77        bytes[0] += ALPHABET_MAP[c];
 78        carry = 0;
 79        j = 0;
 80        while (j < bytes.length) {
 81          bytes[j] += carry;
 82          carry = bytes[j] >> 8;
 83          bytes[j] &= 0xff;
 84          ++j;
 85        }
 86        while (carry) {
 87          bytes.push(carry & 0xff);
 88          carry >>= 8;
 89        }
 90        i++;
 91      }
 92      i = 0;
 93      while (string[i] === "1" && i < string.length - 1) {
 94        bytes.push(0);
 95        i++;
 96      }
 97      return new (typeof Uint8Array !== "undefined" && Uint8Array !== null ? Uint8Array : Buffer)(bytes.reverse());
 98    };
 99  
100  }).call(this);