This is an FFI-based BLAKE3 cryptographic hash library that provides high-performance BLAKE3 hash computation for LuaJIT environments.
LuaJITwithstring.bufferAPIblake3C library (pre-installed)
- Supports standard BLAKE3 hash computation
- Supports keyed hashing
- Supports key derivation
- High performance through FFI calls to C implementation
- Arbitrary length output support
- Hexadecimal output format provided
Creates a new BLAKE3 hasher instance.
Parameters:
opts(table, optional): Initialization optionsinit(boolean): Whether to initialize immediatelykey(string): 32-byte key for keyed hashing modecontext(string): Context string for key derivation mode
Returns:
- BLAKE3 hasher object
Initializes a standard BLAKE3 hasher.
Initializes a BLAKE3 hasher with a key.
Parameters:
key(string): 32-byte key
Initializes a hasher in key derivation mode with a context string.
Parameters:
context(string): Context string
Adds data to the hasher.
Parameters:
input(string): Data to hashinput_len(number, optional): Length of data, defaults to string length
Returns:
- The hasher object itself (for chaining)
Completes the hash computation and returns the result.
Parameters:
out_len(number, optional): Output length, defaults to 32 bytes
Returns:
- Hash result (binary string)
Completes the hash computation and returns the result in the specified format.
Parameters:
out_len(number, optional): Output length, defaults to 32 byteskind(number, optional): Output format (1=lowercase hex, 2=uppercase hex, others=raw binary)
Completes the hash computation and returns the result in the specified format starting from the specified position.
Parameters:
seek(number): Starting positionout_len(number, optional): Output length, defaults to 32 byteskind(number, optional): Output format (1=lowercase hex, 2=uppercase hex, others=raw binary)
Completes the hash computation and returns the result in hexadecimal format.
Parameters:
out_len(number, optional): Output length, defaults to 32 bytes
Outputs hash result starting from a specified position (supports long output).
Parameters:
seek(number): Starting positionout_len(number): Output length
Outputs hash result starting from a specified position (supports long output) with a callback function and variable arguments.
Parameters:
fn(function): Callback function to handle output, protocol:function(cdata, len, ...)seek(number): Starting positionout_len(number): Output length...(any): Variable arguments to pass to the callback function
Outputs hash result starting from a specified position (supports long output) with a callback function.
Parameters:
fn(function): Callback function to handle output, protocol:function(cdata, len, fn_ctx)fn_ctx(any): Callback function contextseek(number): Starting positionout_len(number): Output length
Resets the hasher state for reuse.
Gets the BLAKE3 library version information.
Directly computes the BLAKE3 hash of data (binary format).
Directly computes the BLAKE3 hash of data (hexadecimal format).
Converts binary data to hexadecimal string.
Gets the version information of the underlying BLAKE3 library.
local blake3 = require "resty.blake3"
-- Create hasher instance
local hasher = blake3.new()
hasher:init()
-- Add data
hasher:update("Hello, World!")
-- Get result
local digest = hasher:finalize()
print("Hash:", blake3.to_hex(digest))
-- Or using shortcut
local digest_hex = blake3.hexdigest("Hello, World!")
print("Hash:", digest_hex)local blake3 = require "resty.blake3"
-- Direct hash computation
local hash1 = blake3.digest("data to hash")
local hex_hash1 = blake3.to_hex(hash1)
-- Direct hexadecimal hash
local hex_hash2 = blake3.hexdigest("data to hash")
-- Custom output length
local long_hash = blake3.digest("data to hash", 64) -- 64-byte outputlocal blake3 = require "resty.blake3"
-- Create 32-byte key
local key = string.rep("k", 32) -- 32 'k' characters
-- Initialize with key
local hasher = blake3.new()
hasher:init_keyed(key)
hasher:update("message to hash")
local keyed_hash = hasher:hexdigest()local blake3 = require "resty.blake3"
-- Key derivation with context
local hasher = blake3.new()
hasher:init_derive_key("my context string")
hasher:update("data to hash")
local derived_hash = hasher:hexdigest()local blake3 = require "resty.blake3"
local hasher = blake3.new()
hasher:init()
hasher:update("data")
-- Generate 100-byte hash output
local long_output = hasher:finalize(100)
print("Length:", #long_output)
-- Using seek functionality for even longer output
local long_output_part1 = hasher:finalize_seek(0, 1000)
local long_output_part2 = hasher:finalize_seek(1000, 2000)
local long_output_part3 = hasher:finalize_seek(2000, 3000)
print("Length:", #long_output_part1, #long_output_part2, #long_output_part3)local blake3 = require "resty.blake3"
local hasher = blake3.new()
hasher:init()
hasher:update("data")
local ctx = {
cmp_str = "some binary string",
}
local function cmp_with_my_hash(raw, sz, fn_ctx)
return #fn_ctx.cmp_str >= sz and string.find(fn_ctx.cmp_str, raw, 1, true) == 1
end
local res = hasher:finalize_callback_with_ctx(cmp_with_my_hash, ctx)
local function cmp_with_my_hash_v1(raw, sz, cmp_str)
return #cmp_str >= sz and string.find(cmp_str, raw, 1, true) == 1
end
local res2 = hasher:finalize_callback(cmp_with_my_hash_v1, nil, nil, ctx.cmp_str)local blake3 = require "resty.blake3"
local result = blake3.new()
:init()
:update("Hello, ")
:update("World!")
:hexdigest()
print("Hash:", result)local blake3 = require "resty.blake3"
local hasher = blake3.new()
hasher:init()
-- First computation
hasher:update("first message")
local hash1 = hasher:hexdigest()
-- Reset and compute second time
hasher:reset()
hasher:update("second message")
local hash2 = hasher:hexdigest()
-- Alternatively, you can reset and update in one step
local hash3 = hasher:reset():update("second message"):hexdigest()- Key length must be exactly 32 bytes
- Output length should not exceed the
32KiBlimit(using finalize_seek for large outputs) - When using in OpenResty environment, ensure the BLAKE3 C library is properly installed
- After using
finalizeordigestfunctions, the hasher state is reset; callresetmethod to continue using