crypto.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /**
  2. * From: https://github.com/pvorb/node-crypt/blob/master/crypt.js
  3. * Copyright © 2011, Paul Vorbach. All rights reserved.
  4. * Copyright © 2009, Jeff Mott. 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 list of
  10. * conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
  12. * conditions and the following disclaimer in the documentation and/or other materials
  13. * provided with the distribution.
  14. * 3. Neither the name Crypto-JS nor the names of its contributors may be used to endorse or
  15. * promote products derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  18. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  19. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  20. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  23. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  24. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. // Convert a byte array to big-endian 32-bit words
  27. export function bytesToWords(bytes) {
  28. const words = []
  29. for (let i = 0, b = 0; i < bytes.length; i++, b += 8)
  30. words[b >>> 5] |= bytes[i] << (24 - (b % 32))
  31. return words
  32. }
  33. // Convert big-endian 32-bit words to a byte array
  34. export function wordsToBytes(words) {
  35. const bytes = []
  36. for (let b = 0; b < words.length * 32; b += 8)
  37. bytes.push((words[b >>> 5] >>> (24 - (b % 32))) & 0xff)
  38. return bytes
  39. }