Skip to content
This repository was archived by the owner on Oct 29, 2019. It is now read-only.

Commit 1be8943

Browse files
Add Junit to v3.1.0
1 parent 94bab7d commit 1be8943

File tree

5 files changed

+337
-63
lines changed

5 files changed

+337
-63
lines changed

.gitignore

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,31 @@ local.properties
3232
#################
3333
## Java
3434
#################
35+
# Compiled class file
3536
*.class
3637
*.jardesc
3738

39+
# Log file
40+
*.log
41+
42+
# BlueJ files
43+
*.ctxt
44+
45+
# Mobile Tools for Java (J2ME)
46+
.mtj.tmp/
47+
48+
# Package Files #
49+
*.jar
50+
*.war
51+
*.nar
52+
*.ear
53+
*.zip
54+
*.tar.gz
55+
*.rar
56+
57+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
58+
hs_err_pid*
59+
3860
#################
3961
## Visual Studio
4062
#################
@@ -132,3 +154,43 @@ Thumbs.db
132154
Desktop.ini
133155

134156
runtime.properties
157+
158+
###############
159+
## Intellij
160+
###############
161+
# User-specific stuff
162+
.idea/
163+
164+
# Gradle and Maven with auto-import
165+
# When using Gradle or Maven with auto-import, you should exclude module files,
166+
# since they will be recreated, and may cause churn. Uncomment if using
167+
# auto-import.
168+
.idea/modules.xml
169+
.idea/*.iml
170+
.idea/modules
171+
*.iml
172+
*.ipr
173+
174+
# CMake
175+
cmake-build-*/
176+
177+
# File-based project format
178+
*.iws
179+
180+
# IntelliJ
181+
out/
182+
183+
# mpeltonen/sbt-idea plugin
184+
.idea_modules/
185+
186+
# JIRA plugin
187+
atlassian-ide-plugin.xml
188+
189+
# Crashlytics plugin (for Android Studio and IntelliJ)
190+
com_crashlytics_export_strings.xml
191+
crashlytics.properties
192+
crashlytics-build.properties
193+
fabric.properties
194+
195+
*.log
196+

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<modelVersion>4.0.0</modelVersion>
33
<artifactId>WaarpDigest</artifactId>
44
<name>Waarp Digest</name>
5-
<version>3.0.6</version>
5+
<version>3.1.0</version>
66
<description>This project allows support of Digest (MD5, SHA1, CRC) for Commons Waarp project.</description>
77
<url>http://waarp.github.com/WaarpDigest</url>
88
<inceptionYear>2009</inceptionYear>
@@ -97,7 +97,7 @@
9797
<source>1.6</source>
9898
<target>1.6</target>
9999
<optimize>true</optimize>
100-
<showDeprecations>true</showDeprecations>
100+
<showDeprecation>true</showDeprecation>
101101
</configuration>
102102
</plugin>
103103
<plugin>

src/test/java/.gitkeep

Whitespace-only changes.
Lines changed: 195 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,214 @@
1+
/*******************************************************************************
2+
* This file is part of Waarp Project (named also Waarp or GG).
3+
*
4+
* Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5+
* tags. See the COPYRIGHT.txt in the distribution for a full listing of
6+
* individual contributors.
7+
*
8+
* All Waarp Project is free software: you can redistribute it and/or
9+
* modify it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or (at your
11+
* option) any later version.
12+
*
13+
* Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14+
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15+
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License along with
18+
* Waarp . If not, see <http://www.gnu.org/licenses/>.
19+
******************************************************************************/
20+
121
package org.waarp.common.digest;
222

3-
import static org.junit.Assert.*;
423
import io.netty.buffer.ByteBuf;
524
import io.netty.buffer.Unpooled;
25+
import org.junit.Test;
26+
import org.waarp.common.digest.FilesystemBasedDigest.DigestAlgo;
627

28+
import java.io.File;
29+
import java.io.FileInputStream;
30+
import java.io.FileWriter;
731
import java.io.IOException;
832
import java.security.NoSuchAlgorithmException;
933

10-
import org.junit.Test;
11-
import org.waarp.common.digest.FilesystemBasedDigest.DigestAlgo;
34+
import static org.junit.Assert.*;
1235

1336
public class FilesystemBasedDigestTest {
14-
private static final String TESTPHRASE = "This is a phrase to test";
15-
private static final byte[] TESTPHRASEBYTES = TESTPHRASE.getBytes();
37+
private static final String TESTPHRASE = "This is a phrase to test";
38+
private static final byte[] TESTPHRASEBYTES = TESTPHRASE.getBytes();
1639

17-
@Test
18-
public void testGetHashByteBufDigestAlgo() {
40+
@Test
41+
public void testGetHashByteBufDigestAlgo() {
1942

20-
try {
21-
for (int j = 0; j < 2; j++) {
22-
for (DigestAlgo algo : DigestAlgo.values()) {
23-
long start = System.currentTimeMillis();
24-
byte[] bmd5 = null;
25-
for (int i = 0; i < 100000; i++) {
26-
FilesystemBasedDigest.setUseFastMd5(false);
27-
FilesystemBasedDigest digest = new FilesystemBasedDigest(algo);
28-
digest.Update(TESTPHRASEBYTES, 0, TESTPHRASEBYTES.length);
29-
bmd5 = digest.Final();
30-
String hex = FilesystemBasedDigest.getHex(bmd5);
31-
assertTrue(algo + " Hex Not Equals", FilesystemBasedDigest.digestEquals(hex, bmd5));
32-
FilesystemBasedDigest.setUseFastMd5(true);
33-
FilesystemBasedDigest digest2 = new FilesystemBasedDigest(algo);
34-
digest2.Update(TESTPHRASEBYTES, 0, TESTPHRASEBYTES.length);
35-
byte[] bmd52 = digest2.Final();
36-
String hex2 = FilesystemBasedDigest.getHex(bmd52);
37-
assertTrue(algo + " Hex Not Equals", FilesystemBasedDigest.digestEquals(hex2, bmd52));
38-
assertTrue(algo + " FastMD5 vs MD5 Not Equals", FilesystemBasedDigest.digestEquals(bmd52, bmd5));
39-
FilesystemBasedDigest.setUseFastMd5(false);
40-
ByteBuf buf = Unpooled.wrappedBuffer(TESTPHRASEBYTES);
41-
byte[] bmd53 = FilesystemBasedDigest.getHash(buf, algo);
42-
buf.release();
43-
String hex3 = FilesystemBasedDigest.getHex(bmd53);
44-
assertTrue(algo + " Hex Not Equals", FilesystemBasedDigest.digestEquals(hex3, bmd53));
45-
assertTrue(algo + " Through ByteBuf vs Direct Not Equals",
46-
FilesystemBasedDigest.digestEquals(bmd53, bmd5));
47-
assertTrue(algo + " FromHex Not Equals",
48-
FilesystemBasedDigest.digestEquals(bmd53, FilesystemBasedDigest.getFromHex(hex3)));
49-
}
50-
long end = System.currentTimeMillis();
51-
System.out.println("Algo: " + algo + " KeyLength: " + bmd5.length + " Time: " + (end - start));
52-
}
53-
}
54-
} catch (NoSuchAlgorithmException e) {
55-
fail(e.getMessage());
56-
} catch (IOException e) {
57-
fail(e.getMessage());
43+
try {
44+
for (DigestAlgo algo : DigestAlgo.values()) {
45+
long start = System.currentTimeMillis();
46+
byte[] bmd5 = null;
47+
for (int i = 0; i < 10000; i++) {
48+
FilesystemBasedDigest.setUseFastMd5(false);
49+
FilesystemBasedDigest digest = new FilesystemBasedDigest(algo);
50+
digest.Update(TESTPHRASEBYTES, 0, TESTPHRASEBYTES.length);
51+
bmd5 = digest.Final();
52+
String hex = FilesystemBasedDigest.getHex(bmd5);
53+
assertTrue(algo + " Hex Not Equals",
54+
FilesystemBasedDigest.digestEquals(hex, bmd5));
55+
digest = new FilesystemBasedDigest(algo);
56+
ByteBuf buf = Unpooled.wrappedBuffer(TESTPHRASEBYTES);
57+
digest.Update(buf);
58+
bmd5 = digest.Final();
59+
hex = FilesystemBasedDigest.getHex(bmd5);
60+
assertTrue(algo + " Hex Not Equals",
61+
FilesystemBasedDigest.digestEquals(hex, bmd5));
62+
FilesystemBasedDigest.setUseFastMd5(true);
63+
FilesystemBasedDigest digest2 = new FilesystemBasedDigest(algo);
64+
digest2.Update(TESTPHRASEBYTES, 0, TESTPHRASEBYTES.length);
65+
byte[] bmd52 = digest2.Final();
66+
String hex2 = FilesystemBasedDigest.getHex(bmd52);
67+
assertTrue(algo + " Hex Not Equals",
68+
FilesystemBasedDigest.digestEquals(hex2, bmd52));
69+
assertTrue(algo + " FastMD5 vs MD5 Not Equals",
70+
FilesystemBasedDigest.digestEquals(bmd52, bmd5));
71+
FilesystemBasedDigest.setUseFastMd5(false);
72+
buf = Unpooled.wrappedBuffer(TESTPHRASEBYTES);
73+
byte[] bmd53 = FilesystemBasedDigest.getHash(buf, algo);
74+
buf.release();
75+
String hex3 = FilesystemBasedDigest.getHex(bmd53);
76+
assertTrue(algo + " Hex Not Equals",
77+
FilesystemBasedDigest.digestEquals(hex3, bmd53));
78+
assertTrue(algo + " Through ByteBuf vs Direct Not Equals",
79+
FilesystemBasedDigest.digestEquals(bmd53, bmd5));
80+
assertTrue(algo + " FromHex Not Equals",
81+
FilesystemBasedDigest.digestEquals(bmd53,
82+
FilesystemBasedDigest
83+
.getFromHex(hex3)));
5884
}
85+
long end = System.currentTimeMillis();
86+
System.out.println(
87+
"Algo: " + algo + " KeyLength: " + bmd5.length + " Time: " +
88+
(end - start));
89+
}
90+
FilesystemBasedDigest.setUseFastMd5(false);
91+
ByteBuf buf = Unpooled.wrappedBuffer(TESTPHRASEBYTES);
92+
byte[] bmd5 = FilesystemBasedDigest.getHashMd5(buf);
93+
byte[] bmd52 = FilesystemBasedDigest.getHash(buf, DigestAlgo.MD5);
94+
assertTrue(DigestAlgo.MD5 + " Hex Not Equals",
95+
FilesystemBasedDigest.digestEquals(bmd52, bmd5));
96+
} catch (NoSuchAlgorithmException e) {
97+
fail(e.getMessage());
98+
} catch (IOException e) {
99+
fail(e.getMessage());
59100
}
101+
}
60102

61-
@Test
62-
public void testPasswdCryptString() {
63-
for (int j = 0; j < 2; j++) {
64-
long start = System.currentTimeMillis();
65-
byte[] bmd5 = null;
66-
for (int i = 0; i < 100000; i++) {
67-
FilesystemBasedDigest.setUseFastMd5(false);
68-
String crypt = FilesystemBasedDigest.passwdCrypt(TESTPHRASE);
69-
byte[] bcrypt = FilesystemBasedDigest.passwdCrypt(TESTPHRASEBYTES);
70-
bmd5 = bcrypt;
71-
assertTrue("Password Hex Not Equals", FilesystemBasedDigest.digestEquals(crypt, bcrypt));
72-
assertTrue("Password Not Equals", FilesystemBasedDigest.equalPasswd(TESTPHRASEBYTES, bcrypt));
73-
assertTrue("Password Not Equals", FilesystemBasedDigest.equalPasswd(TESTPHRASE, crypt));
74-
}
75-
long end = System.currentTimeMillis();
76-
System.out.println("Algo: CRYPT KeyLength: " + bmd5.length + " Time: " + (end - start));
103+
@Test
104+
public void testGetHashFileDigestAlgo() throws IOException {
105+
File file = File.createTempFile("testHash", ".txt", new File("/tmp"));
106+
FileWriter fileWriterBig = new FileWriter(file);
107+
fileWriterBig.write(TESTPHRASE);
108+
fileWriterBig.flush();
109+
fileWriterBig.close();
110+
try {
111+
for (DigestAlgo algo : DigestAlgo.values()) {
112+
long start = System.currentTimeMillis();
113+
byte[] bmd5 = null;
114+
for (int i = 0; i < 1000; i++) {
115+
FilesystemBasedDigest.setUseFastMd5(false);
116+
bmd5 = FilesystemBasedDigest.getHash(file, false, algo);
117+
byte[] bmd52 = FilesystemBasedDigest.getHash(file, true, algo);
118+
FileInputStream stream = new FileInputStream(file);
119+
byte[] bmd53 = FilesystemBasedDigest.getHash(stream, algo);
120+
stream.close();
121+
assertTrue(algo + " Hex Not Equals",
122+
FilesystemBasedDigest.digestEquals(bmd5, bmd52));
123+
assertTrue(algo + " Hex Not Equals",
124+
FilesystemBasedDigest.digestEquals(bmd5, bmd53));
125+
FilesystemBasedDigest.setUseFastMd5(true);
126+
bmd5 = FilesystemBasedDigest.getHash(file, false, algo);
127+
bmd52 = FilesystemBasedDigest.getHash(file, true, algo);
128+
stream = new FileInputStream(file);
129+
bmd53 = FilesystemBasedDigest.getHash(stream, algo);
130+
stream.close();
131+
assertTrue(algo + " Hex Not Equals",
132+
FilesystemBasedDigest.digestEquals(bmd5, bmd52));
133+
assertTrue(algo + " Hex Not Equals",
134+
FilesystemBasedDigest.digestEquals(bmd5, bmd53));
77135
}
136+
long end = System.currentTimeMillis();
137+
System.out.println(
138+
"Algo: " + algo + " KeyLength: " + bmd5.length + " Time: " +
139+
(end - start));
140+
}
141+
} catch (IOException e) {
142+
fail(e.getMessage());
143+
} finally {
144+
file.delete();
78145
}
146+
}
79147

148+
@Test
149+
public void testPasswdCryptString() {
150+
long start = System.currentTimeMillis();
151+
byte[] bmd5 = null;
152+
for (int i = 0; i < 100000; i++) {
153+
FilesystemBasedDigest.setUseFastMd5(false);
154+
String crypt = FilesystemBasedDigest.passwdCrypt(TESTPHRASE);
155+
byte[] bcrypt = FilesystemBasedDigest.passwdCrypt(TESTPHRASEBYTES);
156+
bmd5 = bcrypt;
157+
assertTrue("Password Hex Not Equals",
158+
FilesystemBasedDigest.digestEquals(crypt, bcrypt));
159+
assertTrue("Password Not Equals",
160+
FilesystemBasedDigest.equalPasswd(TESTPHRASEBYTES, bcrypt));
161+
assertTrue("Password Not Equals",
162+
FilesystemBasedDigest.equalPasswd(TESTPHRASE, crypt));
163+
}
164+
long end = System.currentTimeMillis();
165+
System.out.println(
166+
"Algo: CRYPT KeyLength: " + bmd5.length + " Time: " + (end - start));
167+
}
168+
169+
@Test
170+
public void testSpecificMD5() throws IOException {
171+
FilesystemBasedDigest.setUseFastMd5(false);
172+
assertFalse(FilesystemBasedDigest.isUseFastMd5());
173+
FilesystemBasedDigest.initializeMd5(false);
174+
assertFalse(FilesystemBasedDigest.isUseFastMd5());
175+
assertEquals(DigestAlgo.MD5.getByteSize() * 2, DigestAlgo.MD5.getHexSize());
176+
177+
File file = File.createTempFile("testHash", ".txt", new File("/tmp"));
178+
FileWriter fileWriterBig = new FileWriter(file);
179+
fileWriterBig.write(TESTPHRASE);
180+
fileWriterBig.flush();
181+
fileWriterBig.close();
182+
try {
183+
DigestAlgo algo = DigestAlgo.MD5;
184+
long start = System.currentTimeMillis();
185+
byte[] bmd5 = null;
186+
for (int i = 0; i < 1000; i++) {
187+
FilesystemBasedDigest.setUseFastMd5(false);
188+
byte[] bmd51 = FilesystemBasedDigest.getHashMd5Nio(file);
189+
bmd5 = bmd51;
190+
byte[] bmd52 = FilesystemBasedDigest.getHashMd5(file);
191+
192+
193+
assertTrue(algo + " Hex Not Equals",
194+
FilesystemBasedDigest.digestEquals(bmd51, bmd52));
195+
196+
FilesystemBasedDigest.setUseFastMd5(true);
197+
bmd51 = FilesystemBasedDigest.getHashMd5Nio(file);
198+
bmd52 = FilesystemBasedDigest.getHashMd5(file);
199+
assertTrue(algo + " Hex Not Equals",
200+
FilesystemBasedDigest.digestEquals(bmd51, bmd52));
201+
assertTrue(algo + " Hex Not Equals",
202+
FilesystemBasedDigest.digestEquals(bmd5, bmd52));
203+
}
204+
long end = System.currentTimeMillis();
205+
System.out.println(
206+
"Algo: " + algo + " KeyLength: " + bmd5.length + " Time: " +
207+
(end - start));
208+
} catch (IOException e) {
209+
fail(e.getMessage());
210+
} finally {
211+
file.delete();
212+
}
213+
}
80214
}

0 commit comments

Comments
 (0)