不使用外部库(例如CryptoJS)进行HMAC-SHA1签名
创始人
2024-12-29 13:00:36
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

相关内容

热门资讯

安卓换鸿蒙系统会卡吗,体验流畅... 最近手机圈可是热闹非凡呢!不少安卓用户都在议论纷纷,说鸿蒙系统要来啦!那么,安卓手机换上鸿蒙系统后,...
安卓系统拦截短信在哪,安卓系统... 你是不是也遇到了这种情况:手机里突然冒出了很多垃圾短信,烦不胜烦?别急,今天就来教你怎么在安卓系统里...
app安卓系统登录不了,解锁登... 最近是不是你也遇到了这样的烦恼:手机里那个心爱的APP,突然就登录不上了?别急,让我来帮你一步步排查...
安卓系统要维护多久,安卓系统维... 你有没有想过,你的安卓手机里那个陪伴你度过了无数日夜的安卓系统,它究竟要陪伴你多久呢?这个问题,估计...
windows官网系统多少钱 Windows官网系统价格一览:了解正版Windows的购买成本Windows 11官方价格解析微软...
安卓系统如何卸载app,轻松掌... 手机里的App越来越多,是不是感觉内存不够用了?别急,今天就来教你怎么轻松卸载安卓系统里的App,让...
怎么复制照片安卓系统,操作步骤... 亲爱的手机控们,是不是有时候想把自己的手机照片分享给朋友,或者备份到电脑上呢?别急,今天就来教你怎么...
安卓系统应用怎么重装,安卓应用... 手机里的安卓应用突然罢工了,是不是让你头疼不已?别急,今天就来手把手教你如何重装安卓系统应用,让你的...
iwatch怎么连接安卓系统,... 你有没有想过,那款时尚又实用的iWatch,竟然只能和iPhone好上好?别急,今天就来给你揭秘,怎...
安装了Anaconda之后找不... 在安装Anaconda后,如果找不到Jupyter Notebook,可以尝试以下解决方法:检查环境...