用typeScript代码写一个HMACSHA1算法
时间: 2024-05-07 09:16:57 浏览: 14
好的,以下是用 TypeScript 代码实现 HMACSHA1 算法的示例:
```
import * as crypto from "crypto";
function hmacsha1(key: string, message: string): string {
const hashedKey = crypto.createHash("sha1").update(key).digest();
const hmac = crypto.createHmac("sha1", hashedKey);
hmac.update(message);
return hmac.digest("hex");
}
// 使用示例
const key = "myKey";
const message = "Hello, world!";
const signature = hmacsha1(key, message);
console.log("Signature: ", signature);
```
解释一下上面代码的实现:
1. 首先,我们使用 crypto 库的 createHash 方法创建一个 SHA1 哈希实例,对 key 进行一次哈希,得到一个 20 字节的哈希值。
2. 然后,我们使用 createHmac 方法创建一个 HMAC 实例,使用上一步得到的哈希值作为密钥,对 message 进行一次 HMAC 运算。
3. 最后,我们将 HMAC 的输出以十六进制字符串的形式返回,作为签名值。
希望这可以帮助到你!
相关问题
用TypeScript实现HMACSHA1算法
以下是用 TypeScript 实现 HMAC-SHA1 算法的示例代码:
```typescript
function hmacSha1(key: string, data: string): string {
const keyBytes = new TextEncoder().encode(key);
const dataBytes = new TextEncoder().encode(data);
const blocksize = 64;
const ipad = new Uint8Array(blocksize);
const opad = new Uint8Array(blocksize);
for (let i = 0; i < blocksize; i++) {
ipad[i] = 0x36;
opad[i] = 0x5c;
}
const keyLen = keyBytes.length > blocksize ? sha1(keyBytes).length : blocksize;
const paddedKey = new Uint8Array(blocksize);
paddedKey.set(keyBytes.slice(0, keyLen));
const innerBlock = new Uint8Array(blocksize + dataBytes.length);
innerBlock.set(ipad);
innerBlock.set(paddedKey, blocksize);
innerBlock.set(dataBytes, blocksize + keyLen);
const innerHash = sha1(innerBlock);
const outerBlock = new Uint8Array(blocksize + innerHash.length);
outerBlock.set(opad);
outerBlock.set(paddedKey, blocksize);
outerBlock.set(innerHash, blocksize);
return sha1(outerBlock);
}
function sha1(data: Uint8Array): string {
const buffer = new Uint8Array(data);
const words = new Array<number>(80);
let h0 = 0x67452301;
let h1 = 0xefcdab89;
let h2 = 0x98badcfe;
let h3 = 0x10325476;
let h4 = 0xc3d2e1f0;
bufferToWords(buffer, words);
for (let i = 16; i < 80; i++) {
words[i] = rotateLeft(words[i - 3] ^ words[i - 8] ^ words[i - 14] ^ words[i - 16], 1);
}
for (let i = 0; i < 80; i++) {
let f: number;
let k: number;
if (i < 20) {
f = (h1 & h2) | (~h1 & h3);
k = 0x5a827999;
} else if (i < 40) {
f = h1 ^ h2 ^ h3;
k = 0x6ed9eba1;
} else if (i < 60) {
f = (h1 & h2) | (h1 & h3) | (h2 & h3);
k = 0x8f1bbcdc;
} else {
f = h1 ^ h2 ^ h3;
k = 0xca62c1d6;
}
const temp = (rotateLeft(h0, 5) + f + h4 + k + words[i]) >>> 0;
h4 = h3;
h3 = h2;
h2 = rotateLeft(h1, 30);
h1 = h0;
h0 = temp;
}
const result = new Uint8Array(20);
wordsToBuffer([h0, h1, h2, h3, h4], result);
return Array.from(result, byteToHex).join('');
}
function bufferToWords(buffer: Uint8Array, words: Array<number>): void {
for (let i = 0; i < buffer.length; i += 4) {
words[i / 4] = (buffer[i] << 24) | (buffer[i + 1] << 16) | (buffer[i + 2] << 8) | buffer[i + 3];
}
}
function wordsToBuffer(words: Array<number>, buffer: Uint8Array): void {
for (let i = 0; i < buffer.length; i += 4) {
buffer[i] = (words[i / 4] >>> 24) & 0xff;
buffer[i + 1] = (words[i / 4] >>> 16) & 0xff;
buffer[i + 2] = (words[i / 4] >>> 8) & 0xff;
buffer[i + 3] = words[i / 4] & 0xff;
}
}
function rotateLeft(value: number, count: number): number {
return ((value << count) | (value >>> (32 - count))) >>> 0;
}
function byteToHex(byte: number): string {
return ('0' + byte.toString(16)).slice(-2);
}
```
此代码使用了 `TextEncoder` 类将字符串转换为字节数组,因此需要在支持该类的环境中运行。如果需要在不支持 `TextEncoder` 类的环境中运行,可以使用其他方法将字符串转换为字节数组。
用typescript写一个冒泡算法
以下是用 TypeScript 写的冒泡排序算法:
```typescript
function bubbleSort(arr: number[]): number[] {
const len = arr.length;
for (let i = 0; i < len - 1; i++) {
for (let j = 0; j < len - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
}
}
}
return arr;
}
```
这个函数接受一个数字数组作为输入,并返回一个经过冒泡排序后的新数组。在内部,它使用了两层循环来遍历数组,并比较相邻元素的大小,如果前一个元素比后一个元素大,则交换它们的位置。通过这个过程,最大的元素会被逐渐“冒泡”到数组的末尾,直到所有元素都被排序完成。