| Did you know ... | Search Documentation: |
| Pack logtalk -- logtalk-3.99.0/library/hashes/NOTES.md |
This file is part of Logtalk https://logtalk.org/ SPDX-FileCopyrightText: 1998-2026 Paulo Moura <pmoura@logtalk.org> SPDX-License-Identifier: Apache-2.0
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
hashes
The hashes library provides portable implementations of several commonly
used hashing algorithms. All hash objects implement the hash_protocol
protocol by providing a hash/2 predicate that takes a list of bytes and
returns the computed hash as a lowercase hexadecimal atom.
The library implements the following hashing algorithms:
djb2_32)djb2_64)sdbm_32)sdbm_64)fnv1a_32)fnv1a_64)siphash_2_4)crc32_reflected(Polynomial))crc32_non_reflected(Polynomial, Initial, FinalXor, AppendLength))crc32b)crc32c)crc32posix)crc32mpeg2)crc32bzip2)crc32q)murmurhash3_x86_32)murmurhash3_x86_128)murmurhash3_x64_128)sha3_224)sha3_256)sha3_384)sha3_512)shake128(OutputBytes))shake256(OutputBytes))md5)sha1)sha256)
The djb2_64, sdbm_64, fnv1a_64, siphash_2_4, murmurhash3_x86_128,
murmurhash3_x64_128, sha3_224, sha3_256, sha3_384, sha3_512,
shake128(OutputBytes), shake256(OutputBytes), sha1, and sha256
objects are only loaded on backend Prolog compilers supporting unbounded
integer arithmetic.
The SHAKE objects are parametric extensible-output functions. Pass the number of output bytes to generate when constructing the object.
The crc32_reflected(Polynomial) object implements a reflected CRC-32 family using
initial value 0xFFFFFFFF and final xor value 0xFFFFFFFF, where
Polynomial is the reflected CRC-32 polynomial.
The crc32_non_reflected(Polynomial, Initial, FinalXor, AppendLength) object
implements a non-reflected CRC-32 family using a canonical polynomial,
configurable initial and final xor values, and an AppendLength flag that
controls whether the message length is appended as little-endian bytes.
The crc32b object implements the CRC-32/ISO-HDLC variant, also widely used
by Ethernet, gzip, and PKZip. It uses reflected input/output processing, the
reflected polynomial 0xEDB88320 (equivalent to the canonical polynomial
0x04C11DB7), initial value 0xFFFFFFFF, and final xor value 0xFFFFFFFF.
The crc32c object implements the CRC-32C/Castagnoli variant using reflected
input/output processing, the reflected polynomial 0x82F63B78 (equivalent to
the canonical polynomial 0x1EDC6F41), initial value 0xFFFFFFFF, and final
xor value 0xFFFFFFFF.
The crc32posix object implements the CRC-32/POSIX variant used by the
standard cksum utility. It is an instance of the non-reflected
crc32_non_reflected(Polynomial, Initial, FinalXor, AppendLength) family:
it uses the canonical polynomial 0x04C11DB7,
initial value 0x00000000, processes bits most-significant first, appends the
message length as little-endian bytes, and applies a final xor value of
0xFFFFFFFF.
The crc32mpeg2 object implements the CRC-32/MPEG-2 variant using the
canonical polynomial 0x04C11DB7, initial value 0xFFFFFFFF, no appended
length bytes, and final xor value 0x00000000.
The crc32bzip2 object implements the CRC-32/BZIP2 variant using the
canonical polynomial 0x04C11DB7, initial value 0xFFFFFFFF, no appended
length bytes, and final xor value 0xFFFFFFFF.
The crc32q object implements the CRC-32Q variant, also used by AIXM-style
formats, using the canonical polynomial 0x814141AB, initial value
0x00000000, no appended length bytes, and final xor value 0x00000000.
The siphash_2_4 object uses the standard reference key `00 01 02 ... 0f`.
For custom keys, use the parametric object siphash_2_4(Key) where Key
is a list of 16 bytes.
The implementations of the hashing algorithms make no attempt to validate
that the input is a list of bytes. When necessary, use the types library
`type::check(list(byte), Input)` goal before calling the hash/2 predicate.
Open the [../../apis/library_index.html#hashes](../../apis/library_index.html#hashes) link in a web browser.
To load all entities in this library, load the loader.lgt file:
| ?- logtalk_load(hashes(loader)).
To test this library predicates, load the tester.lgt file:
| ?- logtalk_load(hashes(tester)).
Compute the SHA-256 hash of a text message:
| ?- atom_codes('The quick brown fox jumps over the lazy dog', Bytes),
sha256::hash(Bytes, Hash).
Bytes = [84,104,101,32,113,117,105,99,107,32,98,114,111,119,110,32,102,111,120,32,106,117,109,112,115,32,111,118,101,114,32,116,104,101,32,108,97,122,121,32,100,111,103],
Hash = 'd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592'
yes
Compute a 32-byte SHAKE128 digest:
| ?- atom_codes('The quick brown fox jumps over the lazy dog', Bytes),
shake128(32)::hash(Bytes, Hash).
Bytes = [84,104,101,32,113,117,105,99,107,32,98,114,111,119,110,32,102,111,120,32,106,117,109,112,115,32,111,118,101,114,32,116,104,101,32,108,97,122,121,32,100,111,103],
Hash = 'f4202e3c5852f9182a0430fd8144f0a74b95e7417ecae17db0f8cfeed0e3e66e'
yes
Compute the CRC-32/ISO-HDLC checksum for the standard 123456789 test vector:
| ?- atom_codes('123456789', Bytes), crc32b::hash(Bytes, Hash).
Bytes = [49,50,51,52,53,54,55,56,57],
Hash = 'cbf43926'
yes
Compute the CRC-32C/Castagnoli checksum for the standard 123456789 test vector:
| ?- atom_codes('123456789', Bytes), crc32c::hash(Bytes, Hash).
Bytes = [49,50,51,52,53,54,55,56,57],
Hash = 'e3069283'
yes
Compute the CRC-32/POSIX checksum for the standard 123456789 test vector:
| ?- atom_codes('123456789', Bytes), crc32posix::hash(Bytes, Hash).
Bytes = [49,50,51,52,53,54,55,56,57],
Hash = '377a6011'
yes
Compute the CRC-32/MPEG-2 checksum for the standard 123456789 test vector:
| ?- atom_codes('123456789', Bytes), crc32mpeg2::hash(Bytes, Hash).
Bytes = [49,50,51,52,53,54,55,56,57],
Hash = '0376e6e7'
yes
Compute the CRC-32/BZIP2 checksum for the standard 123456789 test vector:
| ?- atom_codes('123456789', Bytes), crc32bzip2::hash(Bytes, Hash).
Bytes = [49,50,51,52,53,54,55,56,57],
Hash = 'fc891918'
yes
Compute the CRC-32Q checksum for the standard 123456789 test vector:
| ?- atom_codes('123456789', Bytes), crc32q::hash(Bytes, Hash).
Bytes = [49,50,51,52,53,54,55,56,57],
Hash = '3010bf7f'
yes
Use SipHash-2-4 with a custom key on a backend supporting unbounded integer arithmetic:
| ?- siphash_2_4([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15])::hash([0,1,2,3], Hash).
Hash = 'cf2794e0277187b7'
yes