crypt.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. (function() {
  2. var base64map
  3. = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
  4. crypt = {
  5. // Bit-wise rotation left
  6. rotl: function(n, b) {
  7. return (n << b) | (n >>> (32 - b));
  8. },
  9. // Bit-wise rotation right
  10. rotr: function(n, b) {
  11. return (n << (32 - b)) | (n >>> b);
  12. },
  13. // Swap big-endian to little-endian and vice versa
  14. endian: function(n) {
  15. // If number given, swap endian
  16. if (n.constructor == Number) {
  17. return crypt.rotl(n, 8) & 0x00FF00FF | crypt.rotl(n, 24) & 0xFF00FF00;
  18. }
  19. // Else, assume array and swap all items
  20. for (var i = 0; i < n.length; i++)
  21. n[i] = crypt.endian(n[i]);
  22. return n;
  23. },
  24. // Generate an array of any length of random bytes
  25. randomBytes: function(n) {
  26. for (var bytes = []; n > 0; n--)
  27. bytes.push(Math.floor(Math.random() * 256));
  28. return bytes;
  29. },
  30. // Convert a byte array to big-endian 32-bit words
  31. bytesToWords: function(bytes) {
  32. for (var words = [], i = 0, b = 0; i < bytes.length; i++, b += 8)
  33. words[b >>> 5] |= bytes[i] << (24 - b % 32);
  34. return words;
  35. },
  36. // Convert big-endian 32-bit words to a byte array
  37. wordsToBytes: function(words) {
  38. for (var bytes = [], b = 0; b < words.length * 32; b += 8)
  39. bytes.push((words[b >>> 5] >>> (24 - b % 32)) & 0xFF);
  40. return bytes;
  41. },
  42. // Convert a byte array to a hex string
  43. bytesToHex: function(bytes) {
  44. for (var hex = [], i = 0; i < bytes.length; i++) {
  45. hex.push((bytes[i] >>> 4).toString(16));
  46. hex.push((bytes[i] & 0xF).toString(16));
  47. }
  48. return hex.join('');
  49. },
  50. // Convert a hex string to a byte array
  51. hexToBytes: function(hex) {
  52. for (var bytes = [], c = 0; c < hex.length; c += 2)
  53. bytes.push(parseInt(hex.substr(c, 2), 16));
  54. return bytes;
  55. },
  56. // Convert a byte array to a base-64 string
  57. bytesToBase64: function(bytes) {
  58. for (var base64 = [], i = 0; i < bytes.length; i += 3) {
  59. var triplet = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2];
  60. for (var j = 0; j < 4; j++)
  61. if (i * 8 + j * 6 <= bytes.length * 8)
  62. base64.push(base64map.charAt((triplet >>> 6 * (3 - j)) & 0x3F));
  63. else
  64. base64.push('=');
  65. }
  66. return base64.join('');
  67. },
  68. // Convert a base-64 string to a byte array
  69. base64ToBytes: function(base64) {
  70. // Remove non-base-64 characters
  71. base64 = base64.replace(/[^A-Z0-9+\/]/ig, '');
  72. for (var bytes = [], i = 0, imod4 = 0; i < base64.length;
  73. imod4 = ++i % 4) {
  74. if (imod4 == 0) continue;
  75. bytes.push(((base64map.indexOf(base64.charAt(i - 1))
  76. & (Math.pow(2, -2 * imod4 + 8) - 1)) << (imod4 * 2))
  77. | (base64map.indexOf(base64.charAt(i)) >>> (6 - imod4 * 2)));
  78. }
  79. return bytes;
  80. }
  81. };
  82. module.exports = crypt;
  83. })();