1

想知道某处是否有库调用来转换加密库 sha1 返回格式

15 ?- sha_hash('howdy', X , []), atom_codes(Y, X).
X = [239, 66, 186, 177, 25, 29, 162, 114, 241|...],
Y = 'ïBº±\031\\035\¢rñ95÷\214\@\036\=àÁ\032\û'.

想将 X 转换为类似的格式

“A34F890F16”

4

2 回答 2

2
?- sha_hash('howdy', X , []),
 atom_codes(Y, X),
 maplist(\I^format('~16R',[I]),X).

输出

EF42BAB1191DA272F13935F78C401E3DE0C11AFB
X = [239, 66, 186, 177, 25, 29, 162, 114, 241|...],
Y = 'ïBº±\031\\035\¢rñ95÷\214\@\036\=àÁ\032\û'.

但当然,最上面的字符串是模棱两可的......

或者,可以通过这种方式“手动”填充(此处只需 1 个代码)

?- phrase(xinteger(3), L, []),
   (L =[A] -> N = [48,A] ; N = L),
   format('~s',[N]).

输出

03
L = [51],
A = 51,
N = [48, 51] .

xinteger//1 需要这个包括:- [library(http/dcg_basics)].

编辑:我找到了填充的规范字符串:

?- format('~`0t~16R~2|', [15]).
0F
true.

那么现在可以编写原始示例

?- sha_hash('howdy', X , []),
     atom_codes(Y, X),
     maplist(\I^format('~`0t~16R~2|',[I]),X).

这输出

EF42BAB1191DA272F13935F78C401E3DE0C11AFB
X = [239, 66, 186, 177, 25, 29, 162, 114, 241|...],
Y = 'ïBº±\031\\035\¢rñ95÷\214\@\036\=àÁ\032\û'.

输出可以很容易地被 with_output_to 捕获

atom_to_hex(Atom, Hex) :-
    atom_codes(Atom, Codes),
    with_output_to(Hex, maplist(\I^format('~`0t~16R~2|',[I]), Codes)).

所有这些示例都使用library(lambda)

于 2012-02-24T19:05:10.390 回答
0

Chac,谢谢,你的第一个版本让我走上了正轨,但没有用。对于那些阅读问题的人,这是我的最终版本

asHexByte(X) :- X >= 16, 格式('~R', [X])。

asHexByte(X) :- X < 16, 格式('0~R', [X])。

% 以与 llSHA1String 相同的样式计算 sha1 % sha1string(Atom, SHAString) :- sha_hash(Atom, SHAList, []), with_output_to(codes(SHAString) , maplist(asHexByte , SHAList))。

于 2012-02-25T03:38:36.027 回答