sodium.cr/src/sodium/crypto_box/public_key.cr
Didactic Drunk 92ac0ef6d4 Version 0.9.0
Rearrange CryptoBox.
Move Sodium::Error to it's own file.
Requiring individual files is now possible.
Individual require now possible.
2019-07-01 06:47:11 -07:00

36 lines
965 B
Crystal

require "../lib_sodium"
require "../key"
class Sodium::CryptoBox
class PublicKey < Key
KEY_SIZE = LibSodium.crypto_box_publickeybytes
SEAL_SIZE = LibSodium.crypto_box_sealbytes
getter bytes : Bytes
# :nodoc:
# Only used by SecretKey
def initialize
@bytes = Bytes.new KEY_SIZE
end
def initialize(@bytes : Bytes)
if bytes.bytesize != KEY_SIZE
raise ArgumentError.new("Public key must be #{KEY_SIZE} bytes, got #{bytes.bytesize}")
end
end
# Anonymously send messages to a recipient given its public key.
# For authenticated message use `secret_key.box(recipient_public_key).encrypt`.
def encrypt(src)
encrypt src.to_slice
end
def encrypt(src : Bytes, dst : Bytes = Bytes.new(src.bytesize + SEAL_SIZE)) : Bytes
if LibSodium.crypto_box_seal(dst, src, src.bytesize, @bytes) != 0
raise Sodium::Error.new("crypto_box_seal")
end
dst
end
end
end