2012-02-24 108 views
1

想知道是否有一個庫調用某個地方的密碼庫轉換 SHA1返回格式十六進制字符串SWI-Prolog的

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」格式化

回答

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

所有這些示例均使用庫(lambda)

+0

謝謝 - 你的第一個版本不能正常工作,但確實把我放在了rib軌道上。對於那些閱讀這個問題的人來說,這裏是我的版本asHexByte(X): - \t X> = 16, \t format('〜R',[X])。格式('0〜R',[X])。其中,格式爲'0〜R',[X]。 %計算在相同的風格SHA1作爲llSHA1String % sha1string(原子,SHAString): - \t sha_hash(原子,SHAList,[]), \t with_output_to(碼(SHAString),MAPLIST(asHexByte,SHAList) )。 – Anniepoo 2012-02-25 03:36:50

+0

這裏是chac圖書館的鏈接http://www.complang.tuwien.ac.at/ulrich/Prolog-inedit/ISO-Hiord – Anniepoo 2012-02-27 04:19:03

0

謝謝,你的第一個版本把我放在正確的軌道上,但不工作。對於那些閱讀了問題,這是我的最終版本

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

asHexByte(X): - X < 16, 格式。

%計算在相同的風格SHA1作爲llSHA1String % sha1string(原子,SHAString): - sha_hash(原子,SHAList,[]), with_output_to(碼(SHAString),MAPLIST(asHexByte,SHAList) )。