28 lines
524 B
Crystal
28 lines
524 B
Crystal
require "./lib_sodium"
|
|
require "random/secure"
|
|
|
|
module Cox
|
|
class Nonce
|
|
property bytes : Bytes
|
|
|
|
NONCE_LENGTH = LibSodium::NONCE_BYTES
|
|
|
|
def initialize(@bytes : Bytes)
|
|
if bytes.bytesize != NONCE_LENGTH
|
|
raise ArgumentError.new("Nonce must be #{NONCE_LENGTH} bytes, got #{bytes.bytesize}")
|
|
end
|
|
end
|
|
|
|
def self.new
|
|
new(Random::Secure.random_bytes(NONCE_LENGTH))
|
|
end
|
|
|
|
def pointer
|
|
bytes.to_unsafe
|
|
end
|
|
|
|
def pointer(size)
|
|
bytes.pointer(size)
|
|
end
|
|
end
|
|
end
|