Prolog:md5 谓词

Prolog: md5 predicate

我正在尝试编写一个 md5 谓词来验证以下内容:

md5("my string", "my md5").

这种谓词的真值实例是

md5("long live and prosper", "bf1835ce984d2a97d31409394fe00e9a").

我查看了文档,发现了这个:http://www.swi-prolog.org/pldoc/doc_for?object=crypt/2

?- phrase("$", E, _),
   crypt("My password", E),
   format('~s~n', [E]).

无论如何,我无法让它工作。我确定我遗漏了一些东西,因为我是序言中的新手。有什么提示吗?

编辑

为了更好的解释,我假设创建一个类似于此的子句:

md5(P, M):-
   phrase("$", E, _),
   crypt(P, E),
   name(M, E),
   format('~s~n', [E]).

?- md5("long live and prosper", "bf1835ce984d2a97d31409394fe00e9a").
   $AtnbRJvB$cZ4gZvG2Glelv8hfWztcY/
   false.

谢谢

(Prolog 实现:swi-prolog on Mac OSX El Capitan)

虽然已弃用,但您正在寻找的功能是 available

?- use_module(library(semweb/rdf_db)).
true.

?- rdf_atom_md5("long live and prosper", 1, MD5).
MD5 = bf1835ce984d2a97d31409394fe00e9a.

在 SWI-Prolog 中还有

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).

这在 SWI-Prolog C-library 中,必须使用 use_module(library(md5)).

加载

...不幸的是,这在我的 Fedora 24 上不起作用。RPM 包 pl-7.2.3-3.fc24.x86_64 似乎不完整。没有文件 /usr/lib64/swipl-7.2.3/library/md5.pl 确实:

?- use_module(library(md5)).
ERROR: source_sink `library(md5)' does not exist

为什么!!

另一方面,我们有模块"sha" (/usr/lib64/swipl-7.2.3/library/sha.pl)。因为我只想要一个哈希值,所以这似乎足够了:

library(sha): SHA1 and SHA2 Secure Hash Algorithms: The library library(sha) provides Secure Hash Algorihms approved by FIPS (Federal Information Processing Standard).

好的,所以:

?- 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'.

注意!