I have problems with calculating hash in vb.net.
Working code in Java which calculates good hash is
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
public class MyClass {
public static void main(String args[]) {
String test = "line1\nline2";
System.out.println(test);
String testOut = getTest(test);
System.out.println(testOut);
}
public static String getTest(String input) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(input.getBytes());
return Base64.getEncoder().encodeToString(md.digest());
}
catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
}
Output of this code is
line1
line2
aDN24pCCm0gsJlV0XK/6eh3M+hCvqmLawrQt1saND4M=
Not working example in vb.net is below
Dim test As String = "line1\nline2"
Console.WriteLine(test)
Dim mySHA256 As SHA256 = SHA256Managed.Create()
mySHA256.ComputeHash(Encoding.UTF8.GetBytes(test))
Dim out = Convert.ToBase64String(mySHA256.Hash)
Console.WriteLine(out)
Output is below
line1\nline2
RrUKDuWDeHhbuSYAZlamp7dr3ujXi3v9cm8xvOUtdM0=
So I figured out that "\\n" is not a new line for character in vb.net. So i used vbNewLine
Dim test2 As String = "line1" + vbNewLine + "line2"
Console.WriteLine(test2)
Dim mySHA256_2 As SHA256 = SHA256Managed.Create()
mySHA256_2.ComputeHash(Encoding.UTF8.GetBytes(test2))
Dim out2 = Convert.ToBase64String(mySHA256_2.Hash)
Console.WriteLine(out2)
Output is below
line1
line2
0UqRptHG7oO/DHdOvsvubYs5OzldrinuqDnDVNb7qcA=
I tried to use TransformFinalBlock
instead of ComputeHash
Dim test3 As String = "line1" + vbNewLine + "line2"
Console.WriteLine(test3)
Dim mySHA256_3 As SHA256 = SHA256Managed.Create()
mySHA256_3.TransformFinalBlock(Encoding.UTF8.GetBytes(test3), 0, Encoding.UTF8.GetBytes(test3).Length)
Dim out3 = Convert.ToBase64String(mySHA256_3.Hash)
Console.WriteLine(out3)
and the output is the same as previous
line1
line2
0UqRptHG7oO/DHdOvsvubYs5OzldrinuqDnDVNb7qcA=
I suspect that Java's newline "\\n"
is causing problems because it calculates wrong hash. If i have string without "\\n"
then the values are the same. In vb.net i tried to use Environment.NewLine
instead of vbNewLine
and same result
As you say, if the data that you are encoding does not contain a \\n
, then it works fine. So you're problem obviously has nothing to do with the hashing algorithm. You're problem is that you aren't translating the \\n
in your string literal into VB properly so the two input strings are different. If you output the bytes you would see that the Java one has 0x0A
(10 in decimal) and the VB one has 0x0D 0x0A
(13, 10 in decimal) for the new line.
In VB, vbCR
is the constant for \\r
( 0x0D
) and vbLF
is the constant for \\n
( 0x0A
). Environment.NewLine
and vbNewLine
are platform dependent, but in windows they are equivalent to vbCR & vbLF
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.