不使用外部库(例如CryptoJS)进行HMAC-SHA1签名
创始人
2024-12-29 13:00:18
0

以下是使用JavaScript实现HMAC-SHA1签名的方法,不使用外部库(如CryptoJS)。

function hmacSha1(key, message) {
  const blocksize = 64; // HMAC-SHA1 block size is 64 bytes

  // If the key is longer than blocksize, hash it
  if (key.length > blocksize) {
    key = sha1(key);
  }

  // If the key is shorter than blocksize, pad it with zeros
  if (key.length < blocksize) {
    key = key.concat(new Array(blocksize - key.length).fill(0));
  }

  // XOR key with inner and outer padding
  const ipad = new Array(blocksize).fill(0x36);
  const opad = new Array(blocksize).fill(0x5c);
  for (let i = 0; i < key.length; i++) {
    ipad[i] ^= key[i];
    opad[i] ^= key[i];
  }

  // Perform inner hash
  const innerHash = sha1(ipad.concat(message));

  // Perform outer hash
  return sha1(opad.concat(innerHash));
}

function sha1(message) {
  const blocksize = 64; // SHA-1 block size is 64 bytes
  const wordsize = 4; // SHA-1 word size is 4 bytes

  // Convert message to bytes
  if (typeof message === 'string') {
    message = unescape(encodeURIComponent(message));
  }
  const bytes = new Uint8Array(message.length);
  for (let i = 0; i < message.length; i++) {
    bytes[i] = message.charCodeAt(i);
  }

  // Append padding
  const bitLength = bytes.length * 8;
  bytes[bytes.length] = 0x80; // Append 1 bit followed by 7 zeros
  let paddingLength = blocksize - ((bytes.length + 8) % blocksize);
  paddingLength += paddingLength % wordsize;
  const paddingBytes = new Uint8Array(paddingLength + 8);
  paddingBytes.set(new Uint8Array([0x00]), 0); // Append zeros
  paddingBytes.set(new Uint8Array(bytes.length + 8), paddingLength); // Append bit length
  for (let i = 0; i < bytes.length; i++) {
    paddingBytes[i] = bytes[i];
  }
  for (let i = 0; i < 8; i++) {
    paddingBytes[paddingBytes.length - 1 - i] = (bitLength >>> (i * 8)) & 0xFF;
  }

  // Initialize hash values
  const h0 = 0x67452301;
  const h1 = 0xEFCDAB89;
  const h2 = 0x98BADCFE;
  const h3 = 0x10325476;
  const h4 = 0xC3D2E1F0;

  // Helper function
  function rotateLeft(n, s) {
    return (n << s) | (n >>> (32 - s));
  }

  // Initialize array of round constants
  const k = new Uint32Array([
    0x5A827999,
    0x6ED9EBA1,
    0x8F1BBCDC,
    0xCA62C1D6,
  ]);

  // Initialize hash buffer
  const buffer = new Uint32Array(blocksize);
  buffer[0] = h0;
  buffer[1] = h1;
  buffer[2] = h2;
  buffer[3] = h3;
  buffer[4] = h4;

  // Main loop
  for (let i = 0; i < paddingBytes.length; i += blocksize) {
    const words = new Uint32Array(buffer.buffer, 0, 80);

    // Copy block into buffer
    for (let j = 0; j < blocksize; j++) {
      words[j] = (paddingBytes[i + (j * wordsize)] << 24) |
        (paddingBytes[i + (j * wordsize) + 1] << 16) |
        (paddingBytes[i + (j * wordsize) + 2] << 8) |
        (paddingBytes[i + (j * wordsize) + 3]);
    }

    // Extend the sixteen 32-bit words into eighty 32-bit words
    for (let j = 16; j < 80; j++) {
      const word = words[j - 3] ^ words[j - 8] ^ words[j - 14] ^ words[j - 16];
      words

相关内容

热门资讯

记者揭秘!智星菠萝辅助(透视辅... 记者揭秘!智星菠萝辅助(透视辅助)拱趴大菠萝辅助神器,扑克教程(有挂细节);模式供您选择,了解更新找...
一分钟揭秘!约局吧能能开挂(透... 一分钟揭秘!约局吧能能开挂(透视辅助)hhpoker辅助靠谱,2024新版教程(有挂教学);约局吧能...
透视辅助!wepoker模拟器... 透视辅助!wepoker模拟器哪个好用(脚本)hhpoker辅助挂是真的,科技教程(有挂技巧);囊括...
透视代打!hhpkoer辅助器... 透视代打!hhpkoer辅助器视频(辅助挂)pokemmo脚本辅助,2024新版教程(有挂教程);风...
透视了解!约局吧德州真的有透视... 透视了解!约局吧德州真的有透视挂(透视脚本)德州局HHpoker透视脚本,必胜教程(有挂分析);亲,...
六分钟了解!wepoker挂底... 六分钟了解!wepoker挂底牌(透视)德普之星开辅助,详细教程(有挂解密);德普之星开辅助是一种具...
9分钟了解!wpk私人辅助(透... 9分钟了解!wpk私人辅助(透视)hhpoker德州透视,插件教程(有挂教学);风靡全球的特色经典游...
推荐一款!wepoker究竟有... 推荐一款!wepoker究竟有透视(脚本)哈糖大菠萝开挂,介绍教程(有挂技术);囊括全国各种wepo...
每日必备!wepoker有人用... 每日必备!wepoker有人用过(脚本)wpk有那种辅助,线上教程(有挂规律);wepoker有人用...
玩家必备教程!wejoker私... 玩家必备教程!wejoker私人辅助软件(脚本)哈糖大菠萝可以开挂,可靠技巧(有挂神器)申哈糖大菠萝...