sodium.cr/src/sodium/nonce.cr

22 lines
452 B
Crystal
Raw Normal View History

2017-07-12 05:13:52 +02:00
require "./lib_sodium"
2018-02-14 01:39:15 +01:00
require "random/secure"
2017-07-12 05:13:52 +02:00
module Sodium
2017-07-12 05:13:52 +02:00
class Nonce
NONCE_SIZE = LibSodium::NONCE_SIZE
2017-07-12 05:13:52 +02:00
getter bytes : Bytes
delegate to_slice, to: @bytes
2017-07-12 05:13:52 +02:00
def initialize(@bytes : Bytes)
if bytes.bytesize != NONCE_SIZE
raise ArgumentError.new("Nonce must be #{NONCE_SIZE} bytes, got #{bytes.bytesize}")
2017-07-12 05:13:52 +02:00
end
end
def initialize
@bytes = Random::Secure.random_bytes(NONCE_SIZE)
2017-07-12 05:13:52 +02:00
end
end
end