Switch .to_unsafe to .to_slice
Remove use of .pointer
This commit is contained in:
parent
be5b250a22
commit
a0f15b7657
25
scripts/git/pre-commit
Executable file
25
scripts/git/pre-commit
Executable file
@ -0,0 +1,25 @@
|
|||||||
|
#! /bin/sh
|
||||||
|
#
|
||||||
|
# This script ensures Crystal code is correctly formatted before committing it.
|
||||||
|
# It won't apply any format changes automatically.
|
||||||
|
#
|
||||||
|
# Only staged files (the ones to be committed) are being processed, but each file is checked
|
||||||
|
# entirely as it is stored on disc, even parts that are not staged.
|
||||||
|
#
|
||||||
|
# To use this script, it needs to be installed in the local git repository. For example by running
|
||||||
|
# `ln -s scripts/git/pre-commit .git/hooks` in the root folder.
|
||||||
|
#
|
||||||
|
# Called by "git commit" with no arguments. The hook should
|
||||||
|
# exit with non-zero status after issuing an appropriate message if
|
||||||
|
# it wants to stop the commit.
|
||||||
|
|
||||||
|
changed_cr_files=$(git diff --cached --name-only --diff-filter=ACM | grep '\.cr$')
|
||||||
|
|
||||||
|
[ -z "$changed_cr_files" ] && exit 0
|
||||||
|
|
||||||
|
if [ -x bin/crystal ]; then
|
||||||
|
# use bin/crystal wrapper when available to run local compiler build
|
||||||
|
exec bin/crystal tool format --check $changed_cr_files >&2
|
||||||
|
else
|
||||||
|
exec crystal tool format --check $changed_cr_files >&2
|
||||||
|
fi
|
13
src/cox.cr
13
src/cox.cr
@ -11,14 +11,14 @@ module Cox
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
require "./cox/*"
|
require "./cox/**"
|
||||||
|
|
||||||
module Cox
|
module Cox
|
||||||
def self.encrypt(data, nonce : Nonce, recipient_public_key : PublicKey, sender_secret_key : SecretKey)
|
def self.encrypt(data, nonce : Nonce, recipient_public_key : PublicKey, sender_secret_key : SecretKey)
|
||||||
data_buffer = data.to_slice
|
data_buffer = data.to_slice
|
||||||
data_size = data_buffer.bytesize
|
data_size = data_buffer.bytesize
|
||||||
output_buffer = Bytes.new(data_buffer.bytesize + LibSodium::MAC_SIZE)
|
output_buffer = Bytes.new(data_buffer.bytesize + LibSodium::MAC_SIZE)
|
||||||
if LibSodium.crypto_box_easy(output_buffer.to_unsafe, data_buffer, data_size, nonce.pointer, recipient_public_key.pointer, sender_secret_key.pointer) != 0
|
if LibSodium.crypto_box_easy(output_buffer.to_slice, data_buffer, data_size, nonce.to_slice, recipient_public_key.to_slice, sender_secret_key.to_slice) != 0
|
||||||
raise Error.new("crypto_box_easy")
|
raise Error.new("crypto_box_easy")
|
||||||
end
|
end
|
||||||
output_buffer
|
output_buffer
|
||||||
@ -33,7 +33,7 @@ module Cox
|
|||||||
data_buffer = data.to_slice
|
data_buffer = data.to_slice
|
||||||
data_size = data_buffer.bytesize
|
data_size = data_buffer.bytesize
|
||||||
output_buffer = Bytes.new(data_buffer.bytesize - LibSodium::MAC_SIZE)
|
output_buffer = Bytes.new(data_buffer.bytesize - LibSodium::MAC_SIZE)
|
||||||
if LibSodium.crypto_box_open_easy(output_buffer.to_unsafe, data_buffer.to_unsafe, data_size, nonce.pointer, sender_public_key.pointer, recipient_secret_key.pointer) != 0
|
if LibSodium.crypto_box_open_easy(output_buffer.to_slice, data_buffer.to_slice, data_size, nonce.to_slice, sender_public_key.to_slice, recipient_secret_key.to_slice) != 0
|
||||||
raise DecryptionFailed.new("crypto_box_open_easy")
|
raise DecryptionFailed.new("crypto_box_open_easy")
|
||||||
end
|
end
|
||||||
output_buffer
|
output_buffer
|
||||||
@ -44,7 +44,7 @@ module Cox
|
|||||||
message_buffer_size = message_buffer.bytesize
|
message_buffer_size = message_buffer.bytesize
|
||||||
signature_output_buffer = Bytes.new(LibSodium::SIGNATURE_SIZE)
|
signature_output_buffer = Bytes.new(LibSodium::SIGNATURE_SIZE)
|
||||||
|
|
||||||
if LibSodium.crypto_sign_detached(signature_output_buffer.to_unsafe, 0, message_buffer.to_unsafe, message_buffer_size, secret_key.pointer) != 0
|
if LibSodium.crypto_sign_detached(signature_output_buffer.to_slice, 0, message_buffer.to_slice, message_buffer_size, secret_key.to_slice) != 0
|
||||||
raise Error.new("crypto_sign_detached")
|
raise Error.new("crypto_sign_detached")
|
||||||
end
|
end
|
||||||
signature_output_buffer
|
signature_output_buffer
|
||||||
@ -55,12 +55,11 @@ module Cox
|
|||||||
message_buffer = message.to_slice
|
message_buffer = message.to_slice
|
||||||
message_buffer_size = message_buffer.bytesize
|
message_buffer_size = message_buffer.bytesize
|
||||||
|
|
||||||
verified = LibSodium.crypto_sign_verify_detached(signature_buffer.to_unsafe, message_buffer.to_unsafe, message_buffer_size, public_key.pointer)
|
verified = LibSodium.crypto_sign_verify_detached(signature_buffer.to_slice, message_buffer.to_slice, message_buffer_size, public_key.to_slice)
|
||||||
verified.zero?
|
verified.zero?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if Cox::LibSodium.sodium_init == -1
|
if Cox::LibSodium.sodium_init == -1
|
||||||
STDERR.puts("Failed to init libsodium")
|
abort "Failed to init libsodium"
|
||||||
exit(1)
|
|
||||||
end
|
end
|
||||||
|
@ -2,6 +2,8 @@ module Cox
|
|||||||
class Kdf
|
class Kdf
|
||||||
property bytes : Bytes
|
property bytes : Bytes
|
||||||
|
|
||||||
|
delegate to_slice, to: @bytes
|
||||||
|
|
||||||
def initialize(bytes : Bytes)
|
def initialize(bytes : Bytes)
|
||||||
if bytes.bytesize != LibSodium::KDF_KEY_SIZE
|
if bytes.bytesize != LibSodium::KDF_KEY_SIZE
|
||||||
raise ArgumentError.new("bytes must be #{LibSodium::KDF_KEY_SIZE}, got #{bytes.bytesize}")
|
raise ArgumentError.new("bytes must be #{LibSodium::KDF_KEY_SIZE}, got #{bytes.bytesize}")
|
||||||
@ -28,14 +30,6 @@ module Cox
|
|||||||
subkey
|
subkey
|
||||||
end
|
end
|
||||||
|
|
||||||
def pointer
|
|
||||||
bytes.to_unsafe
|
|
||||||
end
|
|
||||||
|
|
||||||
def pointer(size)
|
|
||||||
bytes.pointer(size)
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_base64
|
def to_base64
|
||||||
Base64.encode(bytes)
|
Base64.encode(bytes)
|
||||||
end
|
end
|
||||||
|
@ -2,13 +2,7 @@ module Cox
|
|||||||
abstract class Key
|
abstract class Key
|
||||||
abstract def bytes
|
abstract def bytes
|
||||||
|
|
||||||
def pointer
|
delegate to_slice, to: @bytes
|
||||||
bytes.to_unsafe
|
|
||||||
end
|
|
||||||
|
|
||||||
def pointer(size)
|
|
||||||
bytes.pointer(size)
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_base64
|
def to_base64
|
||||||
Base64.encode(bytes)
|
Base64.encode(bytes)
|
||||||
|
@ -3,10 +3,11 @@ require "random/secure"
|
|||||||
|
|
||||||
module Cox
|
module Cox
|
||||||
class Nonce
|
class Nonce
|
||||||
property bytes : Bytes
|
|
||||||
|
|
||||||
NONCE_SIZE = LibSodium::NONCE_SIZE
|
NONCE_SIZE = LibSodium::NONCE_SIZE
|
||||||
|
|
||||||
|
property bytes : Bytes
|
||||||
|
delegate to_slice, to: @bytes
|
||||||
|
|
||||||
def initialize(@bytes : Bytes)
|
def initialize(@bytes : Bytes)
|
||||||
if bytes.bytesize != NONCE_SIZE
|
if bytes.bytesize != NONCE_SIZE
|
||||||
raise ArgumentError.new("Nonce must be #{NONCE_SIZE} bytes, got #{bytes.bytesize}")
|
raise ArgumentError.new("Nonce must be #{NONCE_SIZE} bytes, got #{bytes.bytesize}")
|
||||||
@ -16,13 +17,5 @@ module Cox
|
|||||||
def self.new
|
def self.new
|
||||||
new(Random::Secure.random_bytes(NONCE_SIZE))
|
new(Random::Secure.random_bytes(NONCE_SIZE))
|
||||||
end
|
end
|
||||||
|
|
||||||
def pointer
|
|
||||||
bytes.to_unsafe
|
|
||||||
end
|
|
||||||
|
|
||||||
def pointer(size)
|
|
||||||
bytes.pointer(size)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -40,7 +40,7 @@ module Cox
|
|||||||
if dst.bytesize != (src.bytesize + MAC_SIZE)
|
if dst.bytesize != (src.bytesize + MAC_SIZE)
|
||||||
raise ArgumentError.new("dst.bytesize must be src.bytesize + MAC_SIZE, got #{dst.bytesize}")
|
raise ArgumentError.new("dst.bytesize must be src.bytesize + MAC_SIZE, got #{dst.bytesize}")
|
||||||
end
|
end
|
||||||
if LibSodium.crypto_secretbox_easy(dst, src, src.bytesize, nonce.pointer, @bytes) != 0
|
if LibSodium.crypto_secretbox_easy(dst, src, src.bytesize, nonce.to_slice, @bytes) != 0
|
||||||
raise Cox::Error.new("crypto_secretbox_easy")
|
raise Cox::Error.new("crypto_secretbox_easy")
|
||||||
end
|
end
|
||||||
dst
|
dst
|
||||||
@ -57,7 +57,7 @@ module Cox
|
|||||||
if dst.bytesize != (src.bytesize - MAC_SIZE)
|
if dst.bytesize != (src.bytesize - MAC_SIZE)
|
||||||
raise ArgumentError.new("dst.bytesize must be src.bytesize - MAC_SIZE, got #{dst.bytesize}")
|
raise ArgumentError.new("dst.bytesize must be src.bytesize - MAC_SIZE, got #{dst.bytesize}")
|
||||||
end
|
end
|
||||||
if LibSodium.crypto_secretbox_open_easy(dst, src, src.bytesize, nonce.pointer, @bytes) != 0
|
if LibSodium.crypto_secretbox_open_easy(dst, src, src.bytesize, nonce.to_slice, @bytes) != 0
|
||||||
raise Cox::DecryptionFailed.new("crypto_secretbox_easy")
|
raise Cox::DecryptionFailed.new("crypto_secretbox_easy")
|
||||||
end
|
end
|
||||||
dst
|
dst
|
||||||
|
Loading…
Reference in New Issue
Block a user