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)). 47 48/** <module> SHA secure hashes 49 50This library provides a lightweight implementation for computing SHA 51secure hashes. A general secure hash interface is provided by 52library(crypto), part of the `ssl` package. 53 54@see library(md5), library(hash_stream) and library(crypto). 55*/ 56 57%! sha_hash(+Data, -Hash, +Options) is det 58% 59% Hash is the SHA hash of Data, The conversion is controlled 60% by Options: 61% 62% * algorithm(+Algorithm) 63% One of =sha1= (default), =sha224=, =sha256=, =sha384= or 64% =sha512= 65% * encoding(+Encoding) 66% If Data is a sequence of character _codes_, this must be 67% translated into a sequence of _bytes_, because that is what 68% the hashing requires. The default encoding is =utf8=. The 69% other meaningful value is =octet=, claiming that Data contains 70% raw bytes. 71% 72% @param Data is either an atom, string or code-list 73% @param Hash is a packed string 74 75%! sha_new_ctx(-NewContext, +Options) is det 76% 77% NewContext is unified with the empty SHA computation context 78% (which includes the Options.) It could later be passed to 79% sha_hash_ctx/4. For Options, see sha_hash/3. 80% 81% @param NewContext is an opaque pure Prolog term that is 82% subject to garbage collection. 83 84%! sha_hash_ctx(+OldContext, +Data, -NewContext, -Hash) is det 85% 86% Hash is the SHA hash of Data. NewContext is the new SHA 87% computation context, while OldContext is the old. OldContext 88% may be produced by a prior invocation of either sha_new_ctx/3 or 89% sha_hash_ctx/4 itself. 90% 91% This predicate allows a SHA function to be computed in chunks, 92% which may be important while working with Metalink (RFC 5854), 93% BitTorrent or similar technologies, or simply with big files. 94 95%! hmac_sha(+Key, +Data, -Hash, +Options) is det 96% 97% For Options, see sha_hash/3. 98 99%! file_sha1(+File, -SHA1:atom) is det. 100% 101% True when SHA1 is the SHA1 hash for the content of File. Options 102% is passed to open/4 and typically used to control whether binary 103% or text encoding must be used. The output is compatible to the 104% =sha1sum= program found in many systems. 105 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). 126 127 128 129%! hash_atom(+HashCodes, -HexAtom) is det. 130% 131% Convert a list of bytes (integers 0..255) into the usual 132% hexadecimal notation. E.g. 133% 134% == 135% ?- sha_hash('SWI-Prolog', Hash, []), 136% hash_atom(Hash, Hex). 137% Hash = [61, 128, 252, 38, 121, 69, 229, 85, 199|...], 138% Hex = '3d80fc267945e555c730403bd0ab0716e2a68c68'. 139% == 140 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)