From a0f15b765707f47fe443f60e810dbd40b78da265 Mon Sep 17 00:00:00 2001 From: Didactic Drunk <1479616+didactic-drunk@users.noreply.github.com> Date: Thu, 27 Jun 2019 17:35:31 -0700 Subject: [PATCH] Switch .to_unsafe to .to_slice Remove use of .pointer --- scripts/git/pre-commit | 25 +++++++++++++++++++++++++ src/cox.cr | 13 ++++++------- src/cox/kdf.cr | 10 ++-------- src/cox/key.cr | 8 +------- src/cox/nonce.cr | 13 +++---------- src/cox/secret_key.cr | 4 ++-- 6 files changed, 39 insertions(+), 34 deletions(-) create mode 100755 scripts/git/pre-commit diff --git a/scripts/git/pre-commit b/scripts/git/pre-commit new file mode 100755 index 0000000..d892c88 --- /dev/null +++ b/scripts/git/pre-commit @@ -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 diff --git a/src/cox.cr b/src/cox.cr index d1965ad..ca2fdda 100644 --- a/src/cox.cr +++ b/src/cox.cr @@ -11,14 +11,14 @@ module Cox end end -require "./cox/*" +require "./cox/**" module Cox def self.encrypt(data, nonce : Nonce, recipient_public_key : PublicKey, sender_secret_key : SecretKey) data_buffer = data.to_slice data_size = data_buffer.bytesize 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") end output_buffer @@ -33,7 +33,7 @@ module Cox data_buffer = data.to_slice data_size = data_buffer.bytesize 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") end output_buffer @@ -44,7 +44,7 @@ module Cox message_buffer_size = message_buffer.bytesize 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") end signature_output_buffer @@ -55,12 +55,11 @@ module Cox message_buffer = message.to_slice 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? end end if Cox::LibSodium.sodium_init == -1 - STDERR.puts("Failed to init libsodium") - exit(1) + abort "Failed to init libsodium" end diff --git a/src/cox/kdf.cr b/src/cox/kdf.cr index 37ee384..61e7cdb 100644 --- a/src/cox/kdf.cr +++ b/src/cox/kdf.cr @@ -2,6 +2,8 @@ module Cox class Kdf property bytes : Bytes + delegate to_slice, to: @bytes + def initialize(bytes : Bytes) if bytes.bytesize != LibSodium::KDF_KEY_SIZE raise ArgumentError.new("bytes must be #{LibSodium::KDF_KEY_SIZE}, got #{bytes.bytesize}") @@ -28,14 +30,6 @@ module Cox subkey end - def pointer - bytes.to_unsafe - end - - def pointer(size) - bytes.pointer(size) - end - def to_base64 Base64.encode(bytes) end diff --git a/src/cox/key.cr b/src/cox/key.cr index 73e1a89..70dfa8e 100644 --- a/src/cox/key.cr +++ b/src/cox/key.cr @@ -2,13 +2,7 @@ module Cox abstract class Key abstract def bytes - def pointer - bytes.to_unsafe - end - - def pointer(size) - bytes.pointer(size) - end + delegate to_slice, to: @bytes def to_base64 Base64.encode(bytes) diff --git a/src/cox/nonce.cr b/src/cox/nonce.cr index 829448f..77c8115 100644 --- a/src/cox/nonce.cr +++ b/src/cox/nonce.cr @@ -3,10 +3,11 @@ require "random/secure" module Cox class Nonce - property bytes : Bytes - NONCE_SIZE = LibSodium::NONCE_SIZE + property bytes : Bytes + delegate to_slice, to: @bytes + def initialize(@bytes : Bytes) if bytes.bytesize != NONCE_SIZE raise ArgumentError.new("Nonce must be #{NONCE_SIZE} bytes, got #{bytes.bytesize}") @@ -16,13 +17,5 @@ module Cox def self.new new(Random::Secure.random_bytes(NONCE_SIZE)) end - - def pointer - bytes.to_unsafe - end - - def pointer(size) - bytes.pointer(size) - end end end diff --git a/src/cox/secret_key.cr b/src/cox/secret_key.cr index 10960af..f29bed0 100644 --- a/src/cox/secret_key.cr +++ b/src/cox/secret_key.cr @@ -40,7 +40,7 @@ module Cox if dst.bytesize != (src.bytesize + MAC_SIZE) raise ArgumentError.new("dst.bytesize must be src.bytesize + MAC_SIZE, got #{dst.bytesize}") 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") end dst @@ -57,7 +57,7 @@ module Cox if dst.bytesize != (src.bytesize - MAC_SIZE) raise ArgumentError.new("dst.bytesize must be src.bytesize - MAC_SIZE, got #{dst.bytesize}") 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") end dst