base58加密解密:

  1. var ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
  2. var ALPHABET_MAP = {}
  3. for(var i = 0; i < ALPHABET.length; i++) {
  4. ALPHABET_MAP[ALPHABET.charAt(i)] = i
  5. }
  6. var BASE = 58
  7.  
  8. function encode(buffer) {
  9. if (buffer.length === 0) return ''
  10.  
  11. var i, j, digits = [0]
  12. for (i = 0; i < buffer.length; i++) {
  13. for (j = 0; j < digits.length; j++) digits[j] <<= 8
  14.  
  15. digits[0] += buffer[i]
  16.  
  17. var carry = 0
  18. for (j = 0; j < digits.length; ++j) {
  19. digits[j] += carry
  20.  
  21. carry = (digits[j] / BASE) | 0
  22. digits[j] %= BASE
  23. }
  24.  
  25. while (carry) {
  26. digits.push(carry % BASE)
  27.  
  28. carry = (carry / BASE) | 0
  29. }
  30. }
  31.  
  32. // deal with leading zeros
  33. for (i = 0; buffer[i] === 0 && i < buffer.length - 1; i++) digits.push(0)
  34.  
  35. return digits.reverse().map(function(digit) { return ALPHABET[digit] }).join('')
  36. }
  37.  
  38. function decode(string) {
  39. if (string.length === 0) return []
  40.  
  41. var i, j, bytes = [0]
  42. for (i = 0; i < string.length; i++) {
  43. var c = string[i]
  44. if (!(c in ALPHABET_MAP)) throw new Error('Non-base58 character')
  45.  
  46. for (j = 0; j < bytes.length; j++) bytes[j] *= BASE
  47. bytes[0] += ALPHABET_MAP[c]
  48.  
  49. var carry = 0
  50. for (j = 0; j < bytes.length; ++j) {
  51. bytes[j] += carry
  52.  
  53. carry = bytes[j] >> 8
  54. bytes[j] &= 0xff
  55. }
  56.  
  57. while (carry) {
  58. bytes.push(carry & 0xff)
  59.  
  60. carry >>= 8
  61. }
  62. }
  63.  
  64. // deal with leading zeros
  65. for (i = 0; string[i] === '1' && i < string.length - 1; i++) bytes.push(0)
  66.  
  67. return bytes.reverse()
  68. }
  69.  
  70. encode(buffer)//加密
  71. decode(string)//解密


字符串转二进制数组:

  1. function stringToBytes(str) {
  2. var ch, st, re = [];
  3. for (var i = 0; i < str.length; i++) {
  4. ch = str.charCodeAt(i); // get char
  5. st = []; // set up "stack"
  6.  
  7. do {
  8. st.push(ch & 0xFF); // push byte to stack
  9. ch = ch >> 8; // shift value down by 1 byte
  10. }
  11.  
  12. while (ch);
  13. // add stack contents to result
  14. // done because chars have "wrong" endianness
  15. re = re.concat(st.reverse());
  16. }
  17. // return an array of bytes
  18. return re;
  19. }
  20.  
  21. stringToBytes(str)


ascii码转字符:

  1. String.fromCharCode(number)


Hash sha256加密输出ascii字符:

  1. function sha256(ascii) {
  2. function rightRotate(value, amount) {
  3. return (value >>> amount) | (value << (32 - amount));
  4. };
  5.  
  6. var mathPow = Math.pow;
  7. var maxWord = mathPow(2, 32);
  8. var lengthProperty = 'length'
  9. var i, j; // Used as a counter across the whole file
  10. var result = ''
  11.  
  12. var words = [];
  13. var asciiBitLength = ascii[lengthProperty] * 8;
  14.  
  15. //* caching results is optional - remove/add slash from front of this line to toggle
  16. // Initial hash value: first 32 bits of the fractional parts of the square roots of the first 8 primes
  17. // (we actually calculate the first 64, but extra values are just ignored)
  18. var hash = sha256.h = sha256.h || [];
  19. // Round constants: first 32 bits of the fractional parts of the cube roots of the first 64 primes
  20. var k = sha256.k = sha256.k || [];
  21. var primeCounter = k[lengthProperty];
  22. /*/
  23. var hash = [], k = [];
  24. var primeCounter = 0;
  25. //*/
  26.  
  27. var isComposite = {};
  28. for (var candidate = 2; primeCounter < 64; candidate++) {
  29. if (!isComposite[candidate]) {
  30. for (i = 0; i < 313; i += candidate) {
  31. isComposite[i] = candidate;
  32. }
  33. hash[primeCounter] = (mathPow(candidate, .5) * maxWord) | 0;
  34. k[primeCounter++] = (mathPow(candidate, 1 / 3) * maxWord) | 0;
  35. }
  36. }
  37.  
  38. ascii += '\x80' // Append Ƈ' bit (plus zero padding)
  39. while (ascii[lengthProperty] % 64 - 56) ascii += '\x00' // More zero padding
  40. for (i = 0; i < ascii[lengthProperty]; i++) {
  41. j = ascii.charCodeAt(i);
  42. if (j >> 8) return; // ASCII check: only accept characters in range 0-255
  43. words[i >> 2] |= j << ((3 - i) % 4) * 8;
  44. }
  45. words[words[lengthProperty]] = ((asciiBitLength / maxWord) | 0);
  46. words[words[lengthProperty]] = (asciiBitLength)
  47.  
  48. // process each chunk
  49. for (j = 0; j < words[lengthProperty];) {
  50. var w = words.slice(j, j += 16); // The message is expanded into 64 words as part of the iteration
  51. var oldHash = hash;
  52. // This is now the undefinedworking hash", often labelled as variables a...g
  53. // (we have to truncate as well, otherwise extra entries at the end accumulate
  54. hash = hash.slice(0, 8);
  55.  
  56. for (i = 0; i < 64; i++) {
  57. var i2 = i + j;
  58. // Expand the message into 64 words
  59. // Used below if
  60. var w15 = w[i - 15], w2 = w[i - 2];
  61.  
  62. // Iterate
  63. var a = hash[0], e = hash[4];
  64. var temp1 = hash[7]
  65. + (rightRotate(e, 6) ^ rightRotate(e, 11) ^ rightRotate(e, 25)) // S1
  66. + ((e & hash[5]) ^ ((~e) & hash[6])) // ch
  67. + k[i]
  68. // Expand the message schedule if needed
  69. + (w[i] = (i < 16) ? w[i] : (
  70. w[i - 16]
  71. + (rightRotate(w15, 7) ^ rightRotate(w15, 18) ^ (w15 >>> 3)) // s0
  72. + w[i - 7]
  73. + (rightRotate(w2, 17) ^ rightRotate(w2, 19) ^ (w2 >>> 10)) // s1
  74. ) | 0
  75. );
  76. // This is only used once, so *could* be moved below, but it only saves 4 bytes and makes things unreadble
  77. var temp2 = (rightRotate(a, 2) ^ rightRotate(a, 13) ^ rightRotate(a, 22)) // S0
  78. + ((a & hash[1]) ^ (a & hash[2]) ^ (hash[1] & hash[2])); // maj
  79.  
  80. hash = [(temp1 + temp2) | 0].concat(hash); // We don't bother trimming off the extra ones, they're harmless as long as we're truncating when we do the slice()
  81. hash[4] = (hash[4] + temp1) | 0;
  82. }
  83.  
  84. for (i = 0; i < 8; i++) {
  85. hash[i] = (hash[i] + oldHash[i]) | 0;
  86. }
  87. }
  88. var resultArr = new Array;
  89. for (i = 0; i < 8; i++) {
  90. for (j = 3; j + 1; j--) {
  91. var b = (hash[i] >> (j * 8)) & 255;
  92. resultArr.push(b);
  93. }
  94. }
  95. var resultAscii = '';
  96. for (i = 0; i < resultArr.length;i++){
  97. resultAscii += String.fromCharCode(resultArr[i]);
  98. }
  99. console.log(resultAscii);
  100. return resultAscii;
  101. },
  102. stringToBytes:function(str) {
  103. var ch, st, re = [];
  104. for (var i = 0; i < str.length; i++) {
  105. ch = str.charCodeAt(i); // get char
  106. st = []; // set up "stack"
  107.  
  108. do {
  109. st.push(ch & 0xFF); // push byte to stack
  110. ch = ch >> 8; // shift value down by 1 byte
  111. }
  112.  
  113. while (ch);
  114. // add stack contents to result
  115. // done because chars have "wrong" endianness
  116. re = re.concat(st.reverse());
  117. }
  118. // return an array of bytes
  119. return re;
  120. }
  121.  
  122. sha256(str)


base64转二进制数组见:https://www.wikimoe.com/?post=107