1/* Part of SWI-Prolog 2 3 Author: Jan Wielemaker 4 E-mail: J.Wielemaker@vu.nl 5 WWW: http://www.swi-prolog.org 6 Copyright (c) 2007-2013, University of Amsterdam 7 VU University Amsterdam 8 All rights reserved. 9 10 Redistribution and use in source and binary forms, with or without 11 modification, are permitted provided that the following conditions 12 are met: 13 14 1. Redistributions of source code must retain the above copyright 15 notice, this list of conditions and the following disclaimer. 16 17 2. Redistributions in binary form must reproduce the above copyright 18 notice, this list of conditions and the following disclaimer in 19 the documentation and/or other materials provided with the 20 distribution. 21 22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 30 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 32 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 POSSIBILITY OF SUCH DAMAGE. 34*/ 35 36:- module(crypto_hash, 37 [ sha_hash/3, % +Data, -Hash, +Options 38 sha_new_ctx/2, % -NewContext, +Options 39 sha_hash_ctx/4, % +OldCtx, +Data, -NewCtx, -Hash 40 hmac_sha/4, % +Key, +Data, -Hash, +Options 41 file_sha1/2, % +File, -SHA1 42 hash_atom/2 % +Hash, -HexAtom 43 ]). 44:- use_module(library(shlib)). 45 46:- use_foreign_library(foreign(sha4pl)).
sha1
(default), sha224
, sha256
, sha384
or
sha512
utf8
. The
other meaningful value is octet
, claiming that Data contains
raw bytes.This predicate allows a SHA function to be computed in chunks, which may be important while working with Metalink (RFC 5854), BitTorrent or similar technologies, or simply with big files.
sha1sum
program found in many systems.106file_sha1(File, Hash) :- 107 setup_call_cleanup( 108 open(File, read, In, [type(binary)]), 109 stream_sha1(In, Hash), 110 close(In)). 111 112stream_sha1(Stream, Hash) :- 113 sha_new_ctx(Ctx0, [encoding(octet)]), 114 update_hash(Stream, Ctx0, _Ctx, 0, HashCodes), 115 hash_atom(HashCodes, Hash). 116 117update_hash(In, Ctx0, Ctx, Hash0, Hash) :- 118 at_end_of_stream(In), 119 !, 120 Ctx = Ctx0, 121 Hash = Hash0. 122update_hash(In, Ctx0, Ctx, _Hash0, Hash) :- 123 read_pending_codes(In, Data, []), 124 sha_hash_ctx(Ctx0, Data, Ctx1, Hash1), 125 update_hash(In, Ctx1, Ctx, Hash1, Hash).
?- sha_hash('SWI-Prolog', Hash, []), hash_atom(Hash, Hex). Hash = [61, 128, 252, 38, 121, 69, 229, 85, 199|...], Hex = '3d80fc267945e555c730403bd0ab0716e2a68c68'.
141hash_atom(Codes, Hash) :- 142 phrase(bytes_hex(Codes), HexCodes), 143 atom_codes(Hash, HexCodes). 144 145bytes_hex([]) --> []. 146bytes_hex([H|T]) --> 147 { High is H>>4, 148 Low is H /\ 0xf, 149 code_type(C0, xdigit(High)), 150 code_type(C1, xdigit(Low)) 151 }, 152 [C0,C1], 153 bytes_hex(T)
SHA secure hashes
This library provides a lightweight implementation for computing SHA secure hashes. A general secure hash interface is provided by
library(crypto)
, part of thessl
package.library(md5)
,library(hash_stream)
andlibrary(crypto)
. */