1 % (c) 2009-2026 Lehrstuhl fuer Softwaretechnik und Programmiersprachen,
2 % Heinrich Heine Universitaet Duesseldorf
3 % This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html)
4
5 :- module(probhash, [hash_term/2, hash_to_atom/2, raw_sha_hash/2, raw_sha_hash_file/3]).
6
7 :- use_module('../../src/module_information.pl').
8
9 :- module_info(group,infrastructure).
10 :- module_info(description,'This is the interface to C code for generating good hashes (SHA-1, 160 bits).').
11
12 :- use_module(probsrc(error_manager),[add_error/4]).
13
14 :- load_files(library(system), [when(compile_time), imports([environ/2])]).
15
16 :- if((predicate_property(load_foreign_resource(_), _),
17 \+ environ(prob_c_probhash,false))).
18 % -Dprob_c_probhash=false
19
20 :- use_module(library(fastrw),[ fast_buf_read/2,
21 fast_buf_write/3 ]).
22
23 foreign_resource(probhash,[sha1,sha1_file]).
24 foreign(sha1,sha1(+address(char),+integer,[-term])).
25 foreign(sha1_file,sha1_file(+atom,[-term])).
26
27 :- dynamic is_initialised/0.
28
29 :- use_module(probsrc(pathes_lib),[safe_load_foreign_resource/2]).
30
31 init_probhash :- is_initialised,!.
32 init_probhash :-
33 safe_load_foreign_resource(probhash,probhash),
34 assertz(is_initialised).
35
36
37 % input: term
38 % output: hash value as a biginteger (the byte order is reversed)
39 hash_term(Term,Hash) :- raw_sha_hash(Term,H), hash_to_int(H,Hash).
40
41 :- use_module(library(file_systems),[file_exists/1]).
42
43 raw_sha_hash_file(Atom,_,Span) :-
44 \+ file_exists(Atom),!,
45 add_error(probhash,'File does not exist: ',Atom,Span),fail.
46 raw_sha_hash_file(Atom,Hash,_) :-
47 init_probhash,
48 sha1_file(Atom,Hash).
49
50
51
52 % input: raw hash
53 % output: hash value as a biginteger (the byte order is reversed)
54 hash_to_int(X,Y) :- hash_to_int(X,0,Y).
55 hash_to_int([],A,A).
56 hash_to_int([H|T],A,R) :- A2 is A * 256 + H, hash_to_int(T,A2,R).
57
58 % raw sha hashing, input is a term, output a raw hash, i.e., a list of 20 bytes
59 raw_sha_hash(Term,Digest) :-
60 init_probhash,
61 fast_buf_write(Term,Len,Addr),
62 sha1(Addr,Len,Digest).
63
64 % input: raw hash value
65 % output: hash value as an atom
66 hash_to_atom(H,ResultAtom) :-
67 raw_sha_hash(H,H2), hash_to_int(H2,X),
68 number_codes(X, C),atom_codes(ResultAtom,C).
69
70 % input: raw hash value
71 % output: hash value as 5 32-bit chunks
72 hash_to_32bit([V1,V2,V3,V4,V5,V6,V7,V8,V9,V10,V11,V12,V13,V14,V15,V16,V17,V18,V19,V20],[H1,H2,H3,H4,H5]) :-
73 hash_to_int([V1,V2,V3,V4],H1),
74 hash_to_int([V5,V6,V7,V8],H2),
75 hash_to_int([V9,V10,V11,V12],H3),
76 hash_to_int([V13,V14,V15,V16],H4),
77 hash_to_int([V17,V18,V19,V20],H5).
78
79 %-----------------------
80 :- else.
81 %-----------------------
82
83 :- write('Using Prolog term_hash instead of probhash C SHA1 extension (can cause collisions!!)'),nl.
84
85 :- use_module(probsrc(hashing),[my_term_hash/2]).
86 hash_term(Term,Hash) :- my_term_hash(Term,Hash).
87 hash_to_atom(Term,Hash) :- my_term_hash(Term,Hash).
88 raw_sha_hash(Term,Hash) :- my_term_hash(Term,Hash). % TODO: convert to list
89
90 raw_sha_hash_file(_,_,Span) :-
91 add_error(probhash,'Not implemented: ',raw_sha_hash_file,Span),fail.
92
93 :- endif.