Packages

Cryptographically secure random key generation for Gleam

Current section

Files

Jump to
apiculture src apiculture@checksum.erl
Raw

src/apiculture@checksum.erl

-module(apiculture@checksum).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/apiculture/checksum.gleam").
-export([crc32/1, crc32_checksum/0, format/3, verify/3]).
-export_type([checksum/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" CRC32 checksum implementation for data integrity verification.\n"
"\n"
" This module is a thin wrapper around the `crc32` package that adapts\n"
" its `Int` return type to the `BitArray` expected by apiculture's key API.\n"
"\n"
" **This is NOT cryptographic authentication** - CRC32 is designed to\n"
" detect accidental data corruption only, not malicious tampering.\n"
"\n"
" For authenticated checksums suitable for security-sensitive applications,\n"
" consider using HMAC-based approaches with a cryptographic hash.\n"
).
-type checksum() :: {checksum, fun((bitstring()) -> bitstring()), binary()}.
-file("src/apiculture/checksum.gleam", 33).
?DOC(" Convert a 32-bit integer to a 4-byte BitArray (little-endian).\n").
-spec int_to_4bytes(integer()) -> bitstring().
int_to_4bytes(N) ->
B0 = erlang:'band'(N, 255),
B1 = erlang:'band'(erlang:'bsr'(N, 8), 255),
B2 = erlang:'band'(erlang:'bsr'(N, 16), 255),
B3 = erlang:'band'(erlang:'bsr'(N, 24), 255),
<<B0, B1, B2, B3>>.
-file("src/apiculture/checksum.gleam", 49).
?DOC(
" Compute a CRC32 checksum and return it as a 4-byte BitArray.\n"
" This is a convenience function for direct checksum computation.\n"
).
-spec crc32(bitstring()) -> bitstring().
crc32(Bytes) ->
crc32_bytes(Bytes).
-file("src/apiculture/checksum.gleam", 27).
?DOC(
" Compute a CRC32 checksum of the given bytes.\n"
" Returns a 4-byte BitArray for compatibility with the Checksum API.\n"
).
-spec crc32_bytes(bitstring()) -> bitstring().
crc32_bytes(Bytes) ->
Checksum_int = crc32:checksum(Bytes),
int_to_4bytes(Checksum_int).
-file("src/apiculture/checksum.gleam", 43).
?DOC(
" CRC32 checksum algorithm using CRC-32/ISO-HDLC variant.\n"
" This matches the standard CRC-32 used by ZIP, gzip, PNG, and Ethernet.\n"
).
-spec crc32_checksum() -> checksum().
crc32_checksum() ->
{checksum, fun crc32_bytes/1, <<"CRC32"/utf8>>}.
-file("src/apiculture/checksum.gleam", 54).
?DOC(" Format the checksum output through an encoding or alphabet.\n").
-spec format(checksum(), bitstring(), fun((bitstring()) -> binary())) -> binary().
format(Checksum, Bytes, Format_with) ->
Format_with((erlang:element(2, Checksum))(Bytes)).
-file("src/apiculture/checksum.gleam", 63).
?DOC(" Verify that data matches an expected checksum.\n").
-spec verify(checksum(), bitstring(), bitstring()) -> boolean().
verify(Checksum, Bytes, Expected) ->
(erlang:element(2, Checksum))(Bytes) =:= Expected.