2012-03-13 14 views
0

私たちには、UTF-16でエンコードされたパスワードのSHA1ハッシュをBase64 Encodedする必要がある通信プロトコルがあります。私たちは、与えられたJavaの、しかし私たちはLinuxで実行されているJavaScriptの、およびVisual Basicの例は、(RedHatの)linux ascii to utf-16(sha1とbase64)encode

提供されるテスト文字列:[email protected]
最終的な出力:私が試してみましたrBbBKqbJodT5awZal/CSCYF/sFo=

iconv_t conv = iconv_open("UTF-16LE","ASCII"); // open succeeds 
char *from_string=strdup("[email protected]"); 
size_t from_length=strlen(from_string); 
size_t to_length=from_length*3; 
size_t original_to_length=to_length; 

char *to_string=(char*)calloc(1,to_length); 
int convert_return=iconv(conv,&from_string,&from_length,&to_string,&to_length); 
// convert_return is 0 indicating success, to_length is 11, from_length is 0 

to_stringのsha1およびbase64エンコーディングを実行し、長さが22
の出力結果:GCXe7HMDoq/NRqo1WWYJDDYZzP0=

to_stringに通じIループは私が出た場合:

function str2rstr_utf16le(input) 
{ 
    var output = ""; 
    for(var i = 0; i < input.length; i++) 
    output += String.fromCharCode(input.charCodeAt(i) & 0xFF, 
           (input.charCodeAt(i) >>> 8) & 0xFF); 

    return output; 
} 

は、私が何をしないのです。ここで

for (int i=0; i<original_to_length-to_length; ++i) { 
    printf("to_string %d = %x",i,to_string[i]); 
} 

output: 
to_string 0 = 0 
to_string 1 = 0 
to_string 2 = 0 
to_string 3 = 0 
to_string 4 = 0 
to_string 5 = 0 
to_string 6 = 0 
to_string 7 = 0 
to_string 8 = 0 
to_string 9 = 0 
to_string 10 = 0 
to_string 11 = 0 
to_string 12 = 0 
to_string 13 = 0 
to_string 14 = 21 
to_string 15 = 0 
to_string 16 = 0 
to_string 17 = 0 
to_string 18 = 4 
to_string 19 = 7e 
to_string 20 = 13 
to_string 21 = e 

はjavascriptを変換ですか?
ありがとうございました

+0

正しいUTF-16を選んだことはありますか? –

+0

私は11回ループしていることを考慮して、22行が表示されているのには非常に驚いています(original_to_length == 33、to_length == 22)。 – avakar

+0

私のミスタイプは残念です.... to_lengthが11になり、その差は22 – PhilC

答えて

2

私はシェルスクリプトを使用してチェックし、あなたが与えられた結果は、限り、あなたはUTF-16LE(リトルエンディアン)であることをUTF-16を想定すると、確かに正しいようです:BIG-については

$ echo -e $(echo -n '[email protected]' | iconv -f utf-8 -t utf-16le | sha1sum - | egrep -o '[0-9a-f]+' | sed -r 's/(..)/\\x\1/g') | tr -d '\n' | base64 
rBbBKqbJodT5awZal/CSCYF/sFo= 

エンディアン、私はあなたの結果ではないYrAwH9v3d88gjvsg0Hypu2Cfjc8=を取得するので、エンディアンはここでは問題ではないと思います。

man page for iconv(3)状態は:

The iconv function converts one multibyte character at a 
time, and for each character conversion it increments 
*inbuf and decrements *inbytesleft by the number of con­ 
verted input bytes, it increments *outbuf and decrements 
*outbytesleft by the number of converted output bytes, and 
it updates the conversion state contained in cd. 

これはiconvがターゲットバッファポインタ(to_string)を変更することを示唆している - あなたはそれが&to_string、ないto_string自体を渡す理由です。したがって、おそらく処理されたバイト数をiconvの後で、その後の操作(SHA1とBASE64)の前に、to_stringから減算する必要があります。

+0

sedパイプラインが改行を追加します。それが 'K'です。 –

+0

結果を確認する簡単な方法は、このPythonです: 'hashlib.sha1( '[email protected]'.encode(' utf-16le '))。digest()。encode(' base64 ')' –

+0

@JoshLeeありがとう、一定。 –

0

出典:フリー百科事典「ウィキペディア(Wikipedia)」インターネット・プロトコルについては

これらのエンコーディングの名前として、IANAは、 "UTF-16BE"、 "UTF-16" を承認した、と "UTF-16LE"。エイリアスUTF_16またはUTF16は、一部の プログラミング言語またはソフトウェアアプリケーションでは意味がありますが、インターネットプロトコルの標準名ではありません( )。

は私がUTF-16BEUTF-16LE repsectively、ビッグエンディアンとリトルエンディアンのエンコーディングであることを想像してみてください。 UTF-16を使用しているが、入力データに "間違った"エンディアンがあるという点で優れています。

編集:クイック検索は私の疑いを確認しますUTF-16LE is "UTF-16, Little Endian"。あなたの入力データがBig Endianであったという確信は優れています。その場合、すべての「ハイエンド」UTF-16バイトが「ローエンド」バイト位置に配置されます(逆も同様です)。

"UTF-16BE"で期待した結果が得られるかどうかを確認してください。

関連する問題