module RbNaCl::Util
def bin2hex(bytes)
-
(String)
- Tasty, tasty hexadecimal
Parameters:
-
bytes
(String
) -- The bytes to encode
def bin2hex(bytes) bytes.to_s.unpack1("H*") end
def check_hmac_key(string, description)
-
description
(String
) -- Description of the string (used in the error) -
string
(#to_str
) -- The input string
Raises:
-
(RbNaCl::LengthError)
- If the string is empty -
(ArgumentError)
- If we cannot convert to a string with #to_str
def check_hmac_key(string, description) check_string_validation(string) string = string.to_str if string.bytesize.zero? raise LengthError, "#{description} was #{string.bytesize} bytes (Expected more than 0)", caller end string end
def check_length(string, length, description)
-
description
(String
) -- Description of the string (used in the error) -
length
(Integer
) -- The desired length -
string
(String
) -- The string to compare
Raises:
-
(RbNaCl::LengthError)
- If the string is not the right length
def check_length(string, length, description) if string.nil? # code below is runs only in test cases # nil can't be converted to str with #to_str method raise LengthError, "#{description} was nil (Expected #{length.to_int})", caller end if string.bytesize != length.to_int raise LengthError, "#{description} was #{string.bytesize} bytes (Expected #{length.to_int})", caller end true end
def check_string(string, length, description)
-
description
(String
) -- Description of the string (used in the error) -
length
(Integer
) -- The only acceptable length of the string -
string
(#to_str
) -- The input string
Raises:
-
(RbNaCl::LengthError)
- If the string is not the right length -
(ArgumentError)
- If we cannot convert to a string with #to_str
def check_string(string, length, description) check_string_validation(string) string = string.to_s check_length(string, length, description) string end
def check_string_validation(string)
-
string
(#to_str
) -- The input string
Raises:
-
(EncodingError)
- If string have wrong encoding -
(TypeError)
- If string cannot convert to a string with #to_str
def check_string_validation(string) raise TypeError, "can't convert #{string.class} into String with #to_str" unless string.respond_to? :to_str string = string.to_str raise EncodingError, "strings must use BINARY encoding (got #{string.encoding})" if string.encoding != Encoding::BINARY end
def hex2bin(hex)
-
(String)
- crisp and clean bytes
Parameters:
-
hex
(String
) -- hex to decode.
def hex2bin(hex) [hex.to_s].pack("H*") end
def prepend_zeros(n, message)
-
(String)
- a bunch of zeros
Parameters:
-
message
(String
) -- The string to be prepended -
n
(Integer
) -- The number of zeros to prepend
def prepend_zeros(n, message) zeros(n) + message end
def remove_zeros(n, message)
-
(String)
- less a bunch of zeros
Parameters:
-
message
(String
) -- The string to be slice -
n
(Integer
) -- The number of zeros to remove
def remove_zeros(n, message) message.slice!(n, message.bytesize - n) end
def verify16(one, two)
-
(Boolean)
- Well, are they equal?
Parameters:
-
two
(String
) -- String #2 -
one
(String
) -- String #1
def verify16(one, two) return false unless two.bytesize == 16 && one.bytesize == 16 c_verify16(one, two) end
def verify16!(one, two)
-
(Boolean)
- Well, are they equal?
Raises:
-
(ArgumentError)
- If the strings are not equal in length
Parameters:
-
two
(String
) -- String #2 -
one
(String
) -- String #1
def verify16!(one, two) check_length(one, 16, "First message") check_length(two, 16, "Second message") c_verify16(one, two) end
def verify32(one, two)
-
(Boolean)
- Well, are they equal?
Parameters:
-
two
(String
) -- String #2 -
one
(String
) -- String #1
def verify32(one, two) return false unless two.bytesize == 32 && one.bytesize == 32 c_verify32(one, two) end
def verify32!(one, two)
-
(Boolean)
- Well, are they equal?
Raises:
-
(ArgumentError)
- If the strings are not equal in length
Parameters:
-
two
(String
) -- String #2 -
one
(String
) -- String #1
def verify32!(one, two) check_length(one, 32, "First message") check_length(two, 32, "Second message") c_verify32(one, two) end
def verify64(one, two)
-
(Boolean)
- Well, are they equal?
Parameters:
-
two
(String
) -- String #2 -
one
(String
) -- String #1
def verify64(one, two) return false unless two.bytesize == 64 && one.bytesize == 64 c_verify64(one, two) end
def verify64!(one, two)
-
(Boolean)
- Well, are they equal?
Raises:
-
(ArgumentError)
- If the strings are not equal in length
Parameters:
-
two
(String
) -- String #2 -
one
(String
) -- String #1
def verify64!(one, two) check_length(one, 64, "First message") check_length(two, 64, "Second message") c_verify64(one, two) end
def zero_pad(n, message)
-
(String)
- A string, n bytes long
Raises:
-
(RbNaCl::LengthError)
- If the string is too long
Parameters:
-
message
(String
) -- the message to be padded -
n
(Integer
) -- The length of the resulting string
def zero_pad(n, message) len = message.bytesize if len == n message elsif len > n raise LengthError, "String too long for zero-padding to #{n} bytes" else message + zeros(n - len) end end
def zeros(n = 32)
-
(String)
- A nice collection of zeros
Parameters:
-
n
(Integer
) -- the size of the string to make
def zeros(n = 32) zeros = "\0" * n # make sure they're 8-bit zeros, not 7-bit zeros. Otherwise we might get # encoding errors later zeros.respond_to?(:force_encoding) ? zeros.force_encoding("ASCII-8BIT") : zeros end