英文:
Hashing function gives different output when running Maven JUnit tests on Jenkins
问题
以下是您要翻译的内容:
As the title says, have this function which takes as arguments a ByteArrayResource and a String representing the name of the algorithm to use.
I'm using `java.security.MessageDigest` with `GnuCrypto` and `BouncyCastleProvider` providers.
I'm having some issues identifying what the problem could be, both my machine and the Jenkins one are running java 11, here's the code:
Test:
void hashOk{
byte[] bytes = Files.readAllBytes(Path.of("path/to/file.xml"));
String hash1 = "5EDF3285285FDABCFFB7019E2442B31F";
String hash2 = hashService.hash(new ByteArrayResource(bytes), "MD5");
Assertions.assertEquals(hash1, hash2);
}
Service:
public String hash(ByteArrayResource obj, String algorithm) throws NoSuchAlgorithmException {
Security.addProvider(new BouncyCastleProvider());
Security.addProvider(new GnuCrypto());
var md = MessageDigest.getInstance(algorithm);
try (DigestInputStream dis = new DigestInputStream(new BufferedInputStream(obj.getInputStream()), md)) {
byte[] buffer = new byte[1024];
while (dis.read(buffer) != -1) ;
byte[] hash = md.digest();
return DatatypeConverter.printHexBinary(hash).toUpperCase();
} catch (IOException e) {...} }
Am I missing something?
These are the results of the test, where "hash1" is the control string and "hash2" is the generated one:
Local MD5
hash1: 5EDF3285285FDABCFFB7019E2442B31F
hash2: 5EDF3285285FDABCFFB7019E2442B31F
Jenkins MD5
hash1: 5EDF3285285FDABCFFB7019E2442B31F
hash2: 7A7E0BBA2452963746BA4D71EB8CCFA3
I've also tried on a different machine with Jenkins and it gives the same results.
Some solutions I found were just a different arrangement of the same code (and I tried them).
There's one which told me to change the `pom.xml` and set the `forkCount` property to 0 on the `maven-surefire-plugin`, but that gave me an exception because it couldn't find the file for the hashing test (?).
英文:
As the title says, have this function which takes as arguments a ByteArrayResource and a String representing the name of the algorithm to use.
I'm using java.security.MessageDigest
with GnuCrypto
and BouncyCastleProvider
providers.
I'm having some issues identifying what the problem could be, both my machine and the Jenkins one are running java 11, here's the code:
Test:
void hashOk{
byte[] bytes = Files.readAllBytes(Path.of("path/to/file.xml"));
String hash1 = "5EDF3285285FDABCFFB7019E2442B31F";
String hash2 = hashService.hash(new ByteArrayResource(bytes), "MD5");
Assertions.assertEquals(hash1, hash2);
}
Service:
public String hash(ByteArrayResource obj, String algorithm) throws NoSuchAlgorithmException {
Security.addProvider(new BouncyCastleProvider());
Security.addProvider(new GnuCrypto());
var md = MessageDigest.getInstance(algorithm);
try (DigestInputStream dis = new DigestInputStream(new BufferedInputStream(obj.getInputStream()), md)) {
byte[] buffer = new byte[1024];
while (dis.read(buffer) != -1) ;
byte[] hash = md.digest();
return DatatypeConverter.printHexBinary(hash).toUpperCase();
} catch (IOException e) {...} }
Am I missing something?
These are the results of the test, where "hash1" is the control string and "hash2" is the generated one:
Local MD5
hash1: 5EDF3285285FDABCFFB7019E2442B31F
hash2: 5EDF3285285FDABCFFB7019E2442B31F
Jenkins MD5
hash1: 5EDF3285285FDABCFFB7019E2442B31F
hash2: 7A7E0BBA2452963746BA4D71EB8CCFA3
I've also tried on a different machine with Jenkins and it gives the same results.
Some solutions I found were just a different arrangement of the same code (and I tried them).
There's one which told me to change the pom.xml
and set the forkCount
property to 0 on the maven-surefire-plugin
, but that gave me an exception because it couldn't find the file for the hashing test (?).
答案1
得分: 1
我解决了这个问题,问题确实是文件读取方面的问题。
在我将文件内容放在一行并将代码更改为仅在每个测试中读取一次该文件后,一切都正常工作了。
谢谢你的帮助!
英文:
I solved the problem, it really was an issue with the reading of the file.
After I've put the file content in a single line and changed the code to read that file one time only instead of once per test it all worked.
Thanks for the help!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论