【问题标题】:Prolog: md5 predicate序言:md5 谓词
【发布时间】:2015-10-14 07:12:10
【问题描述】:

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

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

这种谓词的真值实例是

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

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

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

无论如何,我无法让它工作。我确定我错过了一些东西,因为我在序言中很新。有什么提示吗?

编辑

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

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.

谢谢

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

【问题讨论】:

  • 好吧,我的解释很糟糕。我的意思是我假设 sn-p 从“万岁和繁荣”生成“bf1835ce984d2a97d31409394fe00e9a”,但事实并非如此(当然,我使用我的短语而不是“我的密码”)。感谢您通知我,我将编辑我的问题。现在清楚了吗?
  • @Boris 谢谢,我用我的 shell 的完整输入和输出更新了这个问题

标签: prolog md5 swi-prolog


【解决方案1】:

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

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

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

【讨论】:

  • 太棒了!你能否解释一下为什么它有效 rdf_atom_md5("long live and prosper", 1, 'bf1835ce984d2a97d31409394fe00e9a')。而这个 rdf_atom_md5("long live and prosper", 1, "bf1835ce984d2a97d31409394fe00e9a") 没有?
  • 参数处理不对称。给定名称,谓词会期望一个原子作为第一个参数,一个原子作为最后一个参数,但是第一个参数的类型可以放宽以接受几个有用的表示。输出固定为 atom,不与字符串统一。
【解决方案2】:

在 SWI-Prolog 中也有

library(md5): MD5 hashes: 从 Prolog 计算 MD5 哈希 细绳。这是一个相当短期的解决方案,等待更多 OpenSSL 的 libcrypto 函数的通用接口。

(其中的一个子章节是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: 图书馆 library(sha) 提供 FIPS(联邦)批准的安全哈希算法 信息处理标准)。

好的,所以:

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

注意!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多