asrauthentication.js 823 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import CryptoJS from './cryptojs.js'
  2. /** 获取签名 start */
  3. function toUint8Array(wordArray) {
  4. // Shortcuts
  5. const words = wordArray.words;
  6. const sigBytes = wordArray.sigBytes;
  7. // Convert
  8. const u8 = new Uint8Array(sigBytes);
  9. for (let i = 0; i < sigBytes; i++) {
  10. u8[i] = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
  11. }
  12. return u8;
  13. }
  14. function Uint8ArrayToString(fileData) {
  15. let dataString = '';
  16. for (let i = 0; i < fileData.length; i++) {
  17. dataString += String.fromCharCode(fileData[i]);
  18. }
  19. return dataString;
  20. }
  21. // 签名函数示例
  22. function signCallback(signStr, secretKey) {
  23. const hash = CryptoJS.HmacSHA1(signStr, secretKey);
  24. const bytes = Uint8ArrayToString(toUint8Array(hash));
  25. return uni.arrayBufferToBase64(toUint8Array(hash));
  26. }
  27. /** 获取签名 end */
  28. export default {
  29. signCallback,
  30. }