Skip to content

[Replacement]: create-hmac #708

@gameroman

Description

@gameroman

Package to replace

create-hmac

Suggested replacement(s)

In Node.js

import { createHmac } from 'node:crypto'

In Browser

const toHex = buf => Array.from(new Uint8Array(buf)).map(b => b.toString(16).padStart(2, '0')).join('')

async function generateHMAC(secret, data) {
  const enc = new TextEncoder()
 
  const key = await crypto.subtle.importKey("raw",
    enc.encode(secret),
    { name: "HMAC", hash: "SHA-256" },
    false, ["sign"])

  const signature = await crypto.subtle.sign("HMAC", key, enc.encode(data))
  
  return toHex(signature)
}

Also, if already installed, can use @noble/hashes

import { hmac } from '@noble/hashes/hmac;
import { sha256 } from '@noble/hashes/sha256'
import { bytesToHex } from '@noble/hashes/utils'

const hash = bytesToHex(hmac(sha256, 'secret-key', 'message-to-sign'))
console.log(hash)

Manifest type

preferred (lighter or more modern alternative package)

Rationale

Native, simpler or more performant alternatives

Availability

No response

Code example (optional)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions