{"library":"sodium-hmac","title":"Sodium HMAC Utility","type":"library","description":"sodium-hmac is a JavaScript utility library for creating Hash-based Message Authentication Codes (HMAC). Currently at stable version 2.1.0, this package provides both a streaming API for processing data in chunks and a simplified one-shot API for common SHA256 and SHA512 HMAC operations. Its key differentiator is the flexibility to integrate custom hash functions, provided they adhere to a specific interface (init, update, final, BYTES, STATEBYTES), allowing users to leverage various cryptographic primitives like Blake2b via external libraries such as `sodium`. Maintained by the Holepunch ecosystem, it focuses on reliable cryptographic primitives for secure data integrity and authentication. The library does not enforce specific external dependencies for its hash functions, making it adaptable to different environments and cryptographic backends.","language":"javascript","status":"active","last_verified":"Tue Apr 21","install":{"commands":["npm install sodium-hmac"],"cli":null},"imports":["import { HMAC } from 'sodium-hmac'","import { sha256 } from 'sodium-hmac'","import { HMAC } from 'sodium-hmac'; const result = HMAC.sha256(data, key);"],"auth":{"required":false,"env_vars":[]},"links":{"homepage":null,"github":"https://github.com/holepunchto/sodium-hmac","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/sodium-hmac","openapi_spec":null,"status_page":null,"smithery":null},"quickstart":{"code":"import { HMAC, sha256, sha512 } from 'sodium-hmac';\nimport b4a from 'b4a'; // A common Buffer-compatible utility for universal environments\n\nconst key = b4a.from('a-very-secret-key-of-at-least-32-bytes');\nconst dataPart1 = b4a.from('This is the first part ');\nconst dataPart2 = b4a.from('and this is the second part of the message.');\n\n// 1. Using the streaming API with SHA256\nconst hmacSha256 = new HMAC(sha256);\nhmacSha256.init(key);\nhmacSha256.update(dataPart1);\nhmacSha256.update(dataPart2);\nconst outputSha256 = hmacSha256.final();\nconsole.log('HMAC-SHA256 (streaming):', outputSha256.toString('hex'));\n\n// 2. Using the simple one-shot API with SHA512\nconst fullData = b4a.concat([dataPart1, dataPart2]);\nconst outputSha512 = HMAC.sha512(fullData, key);\nconsole.log('HMAC-SHA512 (one-shot):', outputSha512.toString('hex'));\n\n// 3. Demonstrating custom hash function integration (conceptual, requires 'sodium' or similar)\n// Assuming 'sodium' is installed and provides a compatible blake2b hash interface.\n/*\nimport sodium from 'sodium-native'; // or 'libsodium-wrappers'\nconst blake2b = {\n  init: (state, key) => sodium.crypto_generichash_init(state, key, 64), // 64 bytes for BLAKE2b\n  update: sodium.crypto_generichash_update,\n  final: (state, out) => sodium.crypto_generichash_final(state, out, 64),\n  BYTES: 64,\n  STATEBYTES: sodium.crypto_generichash_STATEBYTES\n};\n\nconst hmacBlake2b = new HMAC(blake2b);\nhmacBlake2b.init(key);\nhmacBlake2b.update(fullData);\nconst outputBlake2b = hmacBlake2b.final(b4a.alloc(blake2b.BYTES));\nconsole.log('HMAC-BLAKE2b (custom hash):', outputBlake2b.toString('hex'));\n*/\n","lang":"typescript","description":"This quickstart demonstrates both the streaming and one-shot HMAC APIs, using built-in SHA256/SHA512 and conceptually showing how to integrate a custom hash function.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}