在查看原始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;
}
答案 0 :(得分:5)
Java版本中需要& 0xff
,因为在Java中,字节是有符号的。 (有些人认为这是一个错误。)
在C#中bytes are unsigned,所以& 0xff
是不必要的。