SecretKey renamed to CryptoBox::SecretKey PublicKey renamed to CryptoBox::PublicKey KeyPair removed. Use CryptoBox::SecretKey instead. Cox.encrypt was removed. Use `secret_key.pair(...).encrypt` Cox.decrypt was removed. Use `secret_key.pair(...).decrypt`
41 lines
995 B
Crystal
41 lines
995 B
Crystal
require "../lib_sodium"
|
|
|
|
module Cox::CryptoBox
|
|
class SecretKey < Key
|
|
KEY_SIZE = LibSodium::SECRET_KEY_SIZE
|
|
MAC_SIZE = LibSodium::MAC_SIZE
|
|
|
|
getter public_key
|
|
getter bytes : Bytes
|
|
|
|
# Generate a new secret/public key pair.
|
|
def initialize
|
|
pkey = Bytes.new(PublicKey::KEY_SIZE)
|
|
@bytes = Bytes.new(KEY_SIZE)
|
|
@public_key = PublicKey.new pkey
|
|
LibSodium.crypto_box_keypair(pkey, @bytes)
|
|
end
|
|
|
|
# Use existing Secret and Public keys.
|
|
def initialize(@bytes : Bytes, pkey : Bytes)
|
|
if bytes.bytesize != KEY_SIZE
|
|
raise ArgumentError.new("Secret key must be #{KEY_SIZE} bytes, got #{bytes.bytesize}")
|
|
end
|
|
@public_key = PublicKey.new pkey
|
|
end
|
|
|
|
def pair(public_key)
|
|
Pair.new self, public_key
|
|
end
|
|
|
|
# Create a new pair and automatically close when exiting the block.
|
|
def pair(public_key)
|
|
pa = pair public_key
|
|
begin
|
|
yield pa
|
|
ensure
|
|
pa.close
|
|
end
|
|
end
|
|
end
|
|
end
|