sodium.cr/src/cox/nonce.cr
2018-02-13 19:39:15 -05:00

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