A secure memory management class for sodium-native for automatic resource cleanup.
usingconst SafeBuffer = require('safe-sodium-buffer')
{
using safeBuffer = new SafeBuffer(32)
safeBuffer.fill(0x42)
safeBuffer.fill(0xff, 1)
}
const sb = new SafeBuffer(32)
sb.fill(0x42)
sb.destroy()
{
using key = new SafeBuffer(32)
using nonce = new SafeBuffer(24)
sodium.randombytes_buf(key.buffer)
sodium.randombytes_buf(nonce.buffer)
}
new SafeBuffer(size) - Allocates size bytes of secure memorySafeBuffer.from(buffer) - Allocates a buffer from another onebuffer - The allocated Buffer (null if size is 0 or after destroy)size - The allocated size in bytesdestroyed - Boolean indicating if memory has been freeddestroy() - Manually free and zero the memory[Symbol.dispose]() - Automatic cleanup method (called by using)SafeBuffer will expose all classic buffer methods: compare, copy, fill...
MIT