在查看了原始jBCrypt v0.1 C#端口中的一个错误后:BCrypt.net (Related Question)。我决定将新的jBCrypt代码与旧的C#端口进行比较,以寻找差异和潜在的问题,如相关问题的错误。
这是我发现的:
// original java (jBCrypt v0.3):
private static int streamtoword(byte data[], int offp[]) {
int i;
int word = 0;
int off = offp[0];
for (i = 0; i < 4; i++) {
word = (word << 8) | (data[off] & 0xff);
off = (off + 1) % data.length;
}
offp[0] = off;
return word;
}
// port to C# :
private static uint StreamToWord(byte[] data, ref int offset)
{
uint word = 0;
for (int i = 0; i < 4; i++)
{
// note the difference with the omission of "& 0xff"
word = (word << 8) | data[offset];
offset = (offset + 1) % data.Length;
}
return word;
}如果前一个是不正确的,下面的方法可以修复它吗?
private static uint StreamToWord(byte[] data, ref int[] offsetp)
{
uint word = 0;
int offset = offsetp[0];
for (int i = 0; i < 4; i++)
{
word = (word << 8) | (uint)(data[offset] & 0xff);
offset = (offset + 1) % data.Length;
}
offsetp[0] = offset;
return word;
}发布于 2010-02-09 00:45:04
& 0xff在Java版本中是必需的,因为在Java中,字节是有符号的。(有些人认为这是一个bug。)
在C#中使用bytes are unsigned,所以& 0xff是不必要的。
https://stackoverflow.com/questions/2223106
复制相似问题