crypto.js 3.25 KB
/**
 * 仅用于 OSS 上传策略:HMAC-SHA1(UTF-8) + Base64,替换原 CryptoJS 整包
 */

function utf8Bytes(str) {
  return new TextEncoder().encode(str);
}

function concat(a, b) {
  const out = new Uint8Array(a.length + b.length);
  out.set(a, 0);
  out.set(b, a.length);
  return out;
}

function rotl(x, n) {
  return ((x << n) | (x >>> (32 - n))) >>> 0;
}

/** @param {Uint8Array} data */
function sha1(data) {
  const ml = data.length;
  const padZeros = (56 - ((ml + 1) % 64) + 64) % 64;
  const totalLen = ml + 1 + padZeros + 8;
  const buf = new Uint8Array(totalLen);
  buf.set(data);
  buf[ml] = 0x80;
  const bitLen = ml * 8;
  const v = new DataView(buf.buffer);
  v.setUint32(totalLen - 8, Math.floor(bitLen / 0x100000000), false);
  v.setUint32(totalLen - 4, bitLen >>> 0, false);

  const h = new Uint32Array([0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0]);
  const w = new Uint32Array(80);

  for (let i = 0; i < totalLen; i += 64) {
    for (let j = 0; j < 16; j++) {
      w[j] = v.getUint32(i + j * 4, false);
    }
    for (let j = 16; j < 80; j++) {
      w[j] = rotl(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);
    }
    let a = h[0];
    let b = h[1];
    let c = h[2];
    let d = h[3];
    let e = h[4];
    for (let j = 0; j < 80; j++) {
      let f;
      let k;
      if (j < 20) {
        f = (b & c) | (~b & d);
        k = 0x5a827999;
      } else if (j < 40) {
        f = b ^ c ^ d;
        k = 0x6ed9eba1;
      } else if (j < 60) {
        f = (b & c) | (b & d) | (c & d);
        k = 0x8f1bbcdc;
      } else {
        f = b ^ c ^ d;
        k = 0xca62c1d6;
      }
      const t = (rotl(a, 5) + f + e + k + w[j]) >>> 0;
      e = d;
      d = c;
      c = rotl(b, 30) >>> 0;
      b = a;
      a = t;
    }
    h[0] = (h[0] + a) >>> 0;
    h[1] = (h[1] + b) >>> 0;
    h[2] = (h[2] + c) >>> 0;
    h[3] = (h[3] + d) >>> 0;
    h[4] = (h[4] + e) >>> 0;
  }

  const out = new Uint8Array(20);
  const ov = new DataView(out.buffer);
  ov.setUint32(0, h[0], false);
  ov.setUint32(4, h[1], false);
  ov.setUint32(8, h[2], false);
  ov.setUint32(12, h[3], false);
  ov.setUint32(16, h[4], false);
  return out;
}

/** @param {Uint8Array} key @param {Uint8Array} message */
function hmacSha1Raw(key, message) {
  let kb = key;
  if (kb.length > 64) {
    const d = sha1(kb);
    kb = new Uint8Array(64);
    kb.set(d);
  } else if (kb.length < 64) {
    const k2 = new Uint8Array(64);
    k2.set(kb);
    kb = k2;
  }
  const ipad = new Uint8Array(64);
  const opad = new Uint8Array(64);
  for (let i = 0; i < 64; i++) {
    ipad[i] = kb[i] ^ 0x36;
    opad[i] = kb[i] ^ 0x5c;
  }
  const inner = sha1(concat(ipad, message));
  return sha1(concat(opad, inner));
}

function bytesToBase64(bytes) {
  if (typeof wx !== 'undefined' && wx.arrayBufferToBase64) {
    return wx.arrayBufferToBase64(
      bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength)
    );
  }
  let s = '';
  for (let i = 0; i < bytes.length; i++) {
    s += String.fromCharCode(bytes[i]);
  }
  return btoa(s);
}

/**
 * 等价于 CryptoJS: enc.Base64.stringify(Crypto.HmacSHA1(message, secret))
 */
export function hmacSha1Base64(message, secret) {
  const mac = hmacSha1Raw(utf8Bytes(secret), utf8Bytes(message));
  return bytesToBase64(mac);
}

export default { hmacSha1Base64 };