sha1.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * From https://github.com/pvorb/node-sha1/blob/master/sha1.js
  3. * Copyright © 2009, Jeff Mott. All rights reserved.
  4. * Copyright © 2011, Paul Vorbach. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. * 3. Neither the name Crypto-JS nor the names of its contributors may be used to
  15. * endorse or promote products derived from this software without specific prior
  16. * written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  22. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  25. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. import { wordsToBytes, bytesToWords } from './crypto'
  30. export function generateSHA1Hash(inputString) {
  31. const encoder = new TextEncoder()
  32. const uint8Array = encoder.encode(inputString)
  33. const m = bytesToWords(uint8Array)
  34. const l = uint8Array.length * 8
  35. const w = []
  36. let H0 = 1732584193
  37. let H1 = -271733879
  38. let H2 = -1732584194
  39. let H3 = 271733878
  40. let H4 = -1009589776
  41. // Padding
  42. m[l >> 5] |= 0x80 << (24 - (l % 32))
  43. m[(((l + 64) >>> 9) << 4) + 15] = l
  44. for (let i = 0; i < m.length; i += 16) {
  45. const a = H0
  46. const b = H1
  47. const c = H2
  48. const d = H3
  49. const e = H4
  50. for (let j = 0; j < 80; j++) {
  51. if (j < 16) w[j] = m[i + j]
  52. else {
  53. const n = w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16]
  54. w[j] = (n << 1) | (n >>> 31)
  55. }
  56. const t =
  57. ((H0 << 5) | (H0 >>> 27)) +
  58. H4 +
  59. (w[j] >>> 0) +
  60. (j < 20
  61. ? ((H1 & H2) | (~H1 & H3)) + 1518500249
  62. : j < 40
  63. ? (H1 ^ H2 ^ H3) + 1859775393
  64. : j < 60
  65. ? ((H1 & H2) | (H1 & H3) | (H2 & H3)) - 1894007588
  66. : (H1 ^ H2 ^ H3) - 899497514)
  67. H4 = H3
  68. H3 = H2
  69. H2 = (H1 << 30) | (H1 >>> 2)
  70. H1 = H0
  71. H0 = t
  72. }
  73. H0 += a
  74. H1 += b
  75. H2 += c
  76. H3 += d
  77. H4 += e
  78. }
  79. const result = wordsToBytes([H0, H1, H2, H3, H4])
  80. // Convert array of bytes to a hex string
  81. // padStart is used to ensure numbers that are
  82. // less than 16 will still be converted into the two-character format
  83. // For example:
  84. // "5" => "05"
  85. // "a" => "0a"
  86. // "ff" => "ff"
  87. return result.map(b => b.toString(16).padStart(2, '0')).join('')
  88. }