Crc64 (crc_64 v0.1.0)
View SourceImplements AWS S3 compatible CRC64-NVME checksum calculation. Pure Elixir implementation with no external dependencies.
CRC64 is a 64-bit cyclic redundancy check algorithm used for error detection. This implementation uses the polynomial 0x9A6C9329AC4BC9B5 which is compatible with AWS S3's CRC64 implementation.
Installation
Available in Hex, the package can be installed
by adding crc_64
to your list of dependencies in mix.exs
:
def deps do
[
{:crc_64, "~> 0.1.0"}
]
end
Usage
Calculate CRC64 Checksum (as integer)
# Calculate checksum for a binary
checksum = Crc64.calculate("Hello, world!")
# Returns a non-negative integer
Calculate CRC64 Checksum (as Base64 string)
# Calculate Base64-encoded checksum
base64_checksum = Crc64.calculate_base64("Hello, world!")
# Returns a Base64 string like "Ik7A4/gH+LE="
Validate Data against Checksum
# Check if data matches a checksum
data = "Important content"
checksum = "dGhlIGNoZWNrc3VtIGhlcmU=" # previously calculated Base64 checksum
is_valid = Crc64.valid_checksum?(data, checksum)
# Returns true if the calculated checksum matches
Validate File against Checksum
# Validate a file against a known checksum
file_path = "path/to/your/file.txt"
checksum = "dGhlIGNoZWNrc3VtIGhlcmU=" # previously calculated Base64 checksum
is_valid = Crc64.valid_file_checksum?(file_path, checksum)
# Returns true if the file's checksum matches
Documentation can be found at https://hexdocs.pm/crc_64.
Summary
Functions
Calculates the CRC64 checksum for the given binary data.
Calculates the CRC64 checksum for the given binary data and returns it as a Base64 encoded string.
Validates if the given checksum matches the calculated checksum for the data.
Validates if the given checksum matches the calculated checksum for the file contents.
Functions
@spec calculate(binary()) :: non_neg_integer()
Calculates the CRC64 checksum for the given binary data.
Returns the checksum as an integer value.
Examples
iex> Crc64.calculate("123456789")
0xAE8B14860A799888
Calculates the CRC64 checksum for the given binary data and returns it as a Base64 encoded string.
This format is compatible with the AWS S3 CRC64 checksums.
Examples
iex> Crc64.calculate_base64("Hello World!")
"AuUcyF784aU="
Validates if the given checksum matches the calculated checksum for the data.
Returns true
if the checksum is valid, false
otherwise.
Examples
iex> Crc64.valid_checksum?("Hello World!", "AuUcyF784aU=")
true
iex> Crc64.valid_checksum?("Hello World!", "u5WU6IUEP4mKJw==")
false
Validates if the given checksum matches the calculated checksum for the file contents.
Returns true
if the checksum is valid, false
otherwise.
Raises an error if the file does not exist or cannot be read.
Examples
iex> File.write!("test.txt", "Hello World!")
:ok
iex> Crc64.valid_file_checksum?("test.txt", "AuUcyF784aU=")
true
iex> File.rm!("test.txt")
:ok