如何在 deno 中使用 HmacSHA256 创建哈希?
How to create a hash with HmacSHA256 in deno?
我知道如何在 Python 中做到这一点。
#!/usr/bin/python
import sys
import os
import hashlib
import hmac
import base64
secretKey = bytes("passw@rd", 'UTF-8')
message = bytes(f'hello world\nhello deno', 'UTF-8')
encryptedKey = base64.b64encode(hmac.new(secretKey, message, digestmod=hashlib.sha256).digest())
print(encryptedKey)
但我不知道如何在 deno 中做到这一点。
我想要 deno 中上面 python 代码的相同结果。
您可以借助内置 crypto.subtle 工具(自 2021 年年中起可用)创建 HMAC-SHA256 哈希,如下所示:
import { encode } from "https://deno.land/std/encoding/base64.ts"
const message = "hello world\nhello deno"
const encoder = new TextEncoder()
const keyBuf = encoder.encode("passw@rd");
const key = await crypto.subtle.importKey(
"raw",
keyBuf,
{name: "HMAC", hash: "SHA-256"},
true,
["sign", "verify"],
)
const data = encoder.encode(message);
const result = await crypto.subtle.sign("HMAC", key , data.buffer);
console.log(encode(new Uint8Array(result)));
kqfsOD/HMHBRL9F1Si4Y/qo9PCw2csuwXIGZK/P1IWc=
在 Deno 中引入 crypto.subtle 之前,有两种基于外部包的选择:
您可以使用 God Crypto,但这需要额外的 Base64 模块。重要提示:God Crypto 所有者已停止维护该包,因此不建议再使用。
import { hmac } from "https://deno.land/x/god_crypto@v1.4.10/mod.ts"
import * as base64 from "https://deno.land/x/base64@v0.2.1/mod.ts"
let secretKey = "passw@rd"
let message = "hello world\nhello deno"
const result: string = base64.fromUint8Array(hmac("sha256", secretKey, message))
console.log(result)
kqfsOD/HMHBRL9F1Si4Y/qo9PCw2csuwXIGZK/P1IWc=
或者您可以使用更方便的 hmac 模块,它集成了“base64”、“utf8”和“hex”的输出编码:
import { hmac } from "https://deno.land/x/hmac@v2.0.1/mod.ts";
let secretKey = "passw@rd"
let message = "hello world\nhello deno"
const result = hmac("sha256", secretKey , message , "utf8", "base64");
console.log(result)
kqfsOD/HMHBRL9F1Si4Y/qo9PCw2csuwXIGZK/P1IWc=
我知道如何在 Python 中做到这一点。
#!/usr/bin/python
import sys
import os
import hashlib
import hmac
import base64
secretKey = bytes("passw@rd", 'UTF-8')
message = bytes(f'hello world\nhello deno', 'UTF-8')
encryptedKey = base64.b64encode(hmac.new(secretKey, message, digestmod=hashlib.sha256).digest())
print(encryptedKey)
但我不知道如何在 deno 中做到这一点。 我想要 deno 中上面 python 代码的相同结果。
您可以借助内置 crypto.subtle 工具(自 2021 年年中起可用)创建 HMAC-SHA256 哈希,如下所示:
import { encode } from "https://deno.land/std/encoding/base64.ts"
const message = "hello world\nhello deno"
const encoder = new TextEncoder()
const keyBuf = encoder.encode("passw@rd");
const key = await crypto.subtle.importKey(
"raw",
keyBuf,
{name: "HMAC", hash: "SHA-256"},
true,
["sign", "verify"],
)
const data = encoder.encode(message);
const result = await crypto.subtle.sign("HMAC", key , data.buffer);
console.log(encode(new Uint8Array(result)));
kqfsOD/HMHBRL9F1Si4Y/qo9PCw2csuwXIGZK/P1IWc=
在 Deno 中引入 crypto.subtle 之前,有两种基于外部包的选择:
您可以使用 God Crypto,但这需要额外的 Base64 模块。重要提示:God Crypto 所有者已停止维护该包,因此不建议再使用。
import { hmac } from "https://deno.land/x/god_crypto@v1.4.10/mod.ts"
import * as base64 from "https://deno.land/x/base64@v0.2.1/mod.ts"
let secretKey = "passw@rd"
let message = "hello world\nhello deno"
const result: string = base64.fromUint8Array(hmac("sha256", secretKey, message))
console.log(result)
kqfsOD/HMHBRL9F1Si4Y/qo9PCw2csuwXIGZK/P1IWc=
或者您可以使用更方便的 hmac 模块,它集成了“base64”、“utf8”和“hex”的输出编码:
import { hmac } from "https://deno.land/x/hmac@v2.0.1/mod.ts";
let secretKey = "passw@rd"
let message = "hello world\nhello deno"
const result = hmac("sha256", secretKey , message , "utf8", "base64");
console.log(result)
kqfsOD/HMHBRL9F1Si4Y/qo9PCw2csuwXIGZK/P1IWc=