Reputation: 1079
I'm trying to write a md5
predicate that verifies the following:
md5("my string", "my md5").
A truth instance of such predicate would be
md5("long live and prosper", "bf1835ce984d2a97d31409394fe00e9a").
I looked into docs, and I found this: http://www.swi-prolog.org/pldoc/doc_for?object=crypt/2
?- phrase("$1$", E, _),
crypt("My password", E),
format('~s~n', [E]).
anyway, I cannot get it to work. I'm sure I'm missing something as I'm quite new in prolog. Any hint?
EDIT
For a better explanation, I assume to create a clause similar to this:
md5(P, M):-
phrase("$1$", E, _),
crypt(P, E),
name(M, E),
format('~s~n', [E]).
?- md5("long live and prosper", "bf1835ce984d2a97d31409394fe00e9a").
$1$AtnbRJvB$cZ4gZvG2Glelv8hfWztcY/
false.
Thanks
(Prolog implementation: swi-prolog on Mac OSX El Capitan)
Upvotes: 2
Views: 281
Reputation: 15338
In SWI-Prolog there is also
library(md5): MD5 hashes: Compute MD5 hashes from a Prolog string. This is a rather short-term solution waiting for a more general interface to the libcrypto functions of OpenSSL.
(a subchapter of this is md5_hash).
This is inside the SWI-Prolog C-library and it must be loaded using use_module(library(md5)).
... Unfortunately this doesn't work on my Fedora 24. The RPM package pl-7.2.3-3.fc24.x86_64
seems incomplete. There is no file /usr/lib64/swipl-7.2.3/library/md5.pl
and indeed:
?- use_module(library(md5)).
ERROR: source_sink `library(md5)' does not exist
WHY!!
On the other hand, we have module "sha" (/usr/lib64/swipl-7.2.3/library/sha.pl
). As I just want a hash value, this seems good enough:
library(sha): SHA1 and SHA2 Secure Hash Algorithms: The library library(sha) provides Secure Hash Algorihms approved by FIPS (Federal Information Processing Standard).
Ok, so:
?- use_module(library(sha)).
true.
?- sha_hash("Beyond the Aquila Rift",H,[algorithm(sha256),encoding(utf8)]),hash_atom(H,Hex).
H = [122, 123, 130, 89, 90, 210, 207, 106, 48|...],
Hex = '7a7b82595ad2cf6a30c2ee66672f53e0d630d4c8742d914e73c6761edc9186d2'.
?- sha_hash("Beyond the Aquila Rift",H,[algorithm(sha1),encoding(utf8)]),hash_atom(H,Hex).
H = [7, 152, 27, 81, 140, 122, 225, 76, 238|...],
Hex = '07981b518c7ae14cee70563d87d56db53656232c'.
Noice!
Upvotes: 1