SipHash v2.1.1 SipHash.Util

Utility module for minor masking and binary conversions.

Summary

Functions

Applies a 64 bit mask to the passed in number to force it to use only 64 bits. Any bits extending further than the 64th bit are zeroed and dropped

Converts a binary input to an unsigned number using little endian

Chunks a binary input into groups of N, where N is passed in. If a group does not have enough chars to be chunked again, it will be added to the list as is

Formats a resulting hash into a chosen format. Arguments are provided by the options passed in by the user when SipHash.hash/3 is called. The /2 version of this function is overridden by a NIF which uses sprintf to do the any formatting. The Elixir implementation just pattern matches on the format chars and provides an backup. The NIF implementation is roughly 1 microsecond quicker, so it’s worth the override

Loads any NIFs needed for this module. Because we have a valid fallback implementation, we don’t have to exit on failure

Used to quickly determine if NIFs have been loaded for this module. Returns true if it has, false if it hasn’t

Pads a binary input with zeroes. If the provided input is not a binary, simply return the value passed in

Converts a binary input to the provided case. If the provided input is not a binary, simply return the value passed in

Converts a number input to a base-16 output (as a string). If the first arg is not a number, simple return the first argument as is

Functions

apply_mask64(input)

Specs

apply_mask64(number) :: number

Applies a 64 bit mask to the passed in number to force it to use only 64 bits. Any bits extending further than the 64th bit are zeroed and dropped.

Examples

iex> SipHash.Util.apply_mask64(9223372036854775808)
9223372036854775808

iex> SipHash.Util.apply_mask64(92233720368547758077)
18446744073709551613

iex> SipHash.Util.apply_mask64("test_string")
** (FunctionClauseError) no function clause matching in SipHash.Util.apply_mask64/1
bytes_to_long(input)

Specs

bytes_to_long(binary) :: number

Converts a binary input to an unsigned number using little endian.

Examples

iex> SipHash.Util.bytes_to_long(<<169,138,199>>)
13077161

iex> SipHash.Util.bytes_to_long("test_string")
125040764888893876906190196

iex> SipHash.Util.bytes_to_long(5)
** (FunctionClauseError) no function clause matching in SipHash.Util.bytes_to_long/1
chunk_string(str, n)

Specs

chunk_string(binary, number) :: list

Chunks a binary input into groups of N, where N is passed in. If a group does not have enough chars to be chunked again, it will be added to the list as is.

Examples

iex> SipHash.Util.chunk_string("12345678", 4)
["1234","5678"]
format(num, binary)
format(s, bool, arg3)

Formats a resulting hash into a chosen format. Arguments are provided by the options passed in by the user when SipHash.hash/3 is called. The /2 version of this function is overridden by a NIF which uses sprintf to do the any formatting. The Elixir implementation just pattern matches on the format chars and provides an backup. The NIF implementation is roughly 1 microsecond quicker, so it’s worth the override.

Examples

iex> SipHash.Util.format(699588702094987020, false, :upper)
699588702094987020

iex> SipHash.Util.format(699588702094987020, true, :upper)
"09B57037CD3F8F0C"

iex> SipHash.Util.format(699588702094987020, true, :lower)
"09b57037cd3f8f0c"
init()

Loads any NIFs needed for this module. Because we have a valid fallback implementation, we don’t have to exit on failure.

nif_loaded()

Specs

nif_loaded :: true | false

Used to quickly determine if NIFs have been loaded for this module. Returns true if it has, false if it hasn’t.

pad_left(s)

Specs

pad_left(binary) :: binary

Pads a binary input with zeroes. If the provided input is not a binary, simply return the value passed in.

Examples

iex> SipHash.Util.pad_left("12345678")
"0000000012345678"

iex> SipHash.Util.pad_left(12345678)
12345678
to_case(s, arg2)

Specs

to_case(binary, atom) :: binary

Converts a binary input to the provided case. If the provided input is not a binary, simply return the value passed in.

Examples

iex> SipHash.Util.to_case("TEST", :lower)
"test"

iex> SipHash.Util.to_case("test", :upper)
"TEST"

iex> SipHash.Util.to_case(5, :upper)
5
to_hex(n)

Specs

to_hex(number) :: binary

Converts a number input to a base-16 output (as a string). If the first arg is not a number, simple return the first argument as is.

Examples

iex> SipHash.Util.to_hex(1215135325)
"486D7E5D"

iex> SipHash.Util.to_hex("test")
"test"