Skip to content
This repository was archived by the owner on Oct 29, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,31 @@ local.properties
#################
## Java
#################
# Compiled class file
*.class
*.jardesc

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

#################
## Visual Studio
#################
Expand Down Expand Up @@ -132,3 +154,43 @@ Thumbs.db
Desktop.ini

runtime.properties

###############
## Intellij
###############
# User-specific stuff
.idea/

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
.idea/modules.xml
.idea/*.iml
.idea/modules
*.iml
*.ipr

# CMake
cmake-build-*/

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

*.log

4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>WaarpDigest</artifactId>
<name>Waarp Digest</name>
<version>3.0.6</version>
<version>3.1.0</version>
<description>This project allows support of Digest (MD5, SHA1, CRC) for Commons Waarp project.</description>
<url>http://waarp.github.com/WaarpDigest</url>
<inceptionYear>2009</inceptionYear>
Expand Down Expand Up @@ -97,7 +97,7 @@
<source>1.6</source>
<target>1.6</target>
<optimize>true</optimize>
<showDeprecations>true</showDeprecations>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/waarp/common/digest/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/** Provides the version information of Waarp Digest. */
public final class Version {
/** The version identifier. */
public static final String ID = "3.0.6";
public static final String ID = "3.1.0";
/** Prints out the version identifier to stdout. */
public static void main(String[] args) { System.out.println(ID); }
private Version() { super(); }
Expand Down
Empty file added src/test/java/.gitkeep
Empty file.
256 changes: 195 additions & 61 deletions src/test/java/org/waarp/common/digest/FilesystemBasedDigestTest.java
Original file line number Diff line number Diff line change
@@ -1,80 +1,214 @@
/*******************************************************************************
* This file is part of Waarp Project (named also Waarp or GG).
*
* Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
* tags. See the COPYRIGHT.txt in the distribution for a full listing of
* individual contributors.
*
* All Waarp Project is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* Waarp . If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

package org.waarp.common.digest;

import static org.junit.Assert.*;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.junit.Test;
import org.waarp.common.digest.FilesystemBasedDigest.DigestAlgo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;

import org.junit.Test;
import org.waarp.common.digest.FilesystemBasedDigest.DigestAlgo;
import static org.junit.Assert.*;

public class FilesystemBasedDigestTest {
private static final String TESTPHRASE = "This is a phrase to test";
private static final byte[] TESTPHRASEBYTES = TESTPHRASE.getBytes();
private static final String TESTPHRASE = "This is a phrase to test";
private static final byte[] TESTPHRASEBYTES = TESTPHRASE.getBytes();

@Test
public void testGetHashByteBufDigestAlgo() {
@Test
public void testGetHashByteBufDigestAlgo() {

try {
for (int j = 0; j < 2; j++) {
for (DigestAlgo algo : DigestAlgo.values()) {
long start = System.currentTimeMillis();
byte[] bmd5 = null;
for (int i = 0; i < 100000; i++) {
FilesystemBasedDigest.setUseFastMd5(false);
FilesystemBasedDigest digest = new FilesystemBasedDigest(algo);
digest.Update(TESTPHRASEBYTES, 0, TESTPHRASEBYTES.length);
bmd5 = digest.Final();
String hex = FilesystemBasedDigest.getHex(bmd5);
assertTrue(algo + " Hex Not Equals", FilesystemBasedDigest.digestEquals(hex, bmd5));
FilesystemBasedDigest.setUseFastMd5(true);
FilesystemBasedDigest digest2 = new FilesystemBasedDigest(algo);
digest2.Update(TESTPHRASEBYTES, 0, TESTPHRASEBYTES.length);
byte[] bmd52 = digest2.Final();
String hex2 = FilesystemBasedDigest.getHex(bmd52);
assertTrue(algo + " Hex Not Equals", FilesystemBasedDigest.digestEquals(hex2, bmd52));
assertTrue(algo + " FastMD5 vs MD5 Not Equals", FilesystemBasedDigest.digestEquals(bmd52, bmd5));
FilesystemBasedDigest.setUseFastMd5(false);
ByteBuf buf = Unpooled.wrappedBuffer(TESTPHRASEBYTES);
byte[] bmd53 = FilesystemBasedDigest.getHash(buf, algo);
buf.release();
String hex3 = FilesystemBasedDigest.getHex(bmd53);
assertTrue(algo + " Hex Not Equals", FilesystemBasedDigest.digestEquals(hex3, bmd53));
assertTrue(algo + " Through ByteBuf vs Direct Not Equals",
FilesystemBasedDigest.digestEquals(bmd53, bmd5));
assertTrue(algo + " FromHex Not Equals",
FilesystemBasedDigest.digestEquals(bmd53, FilesystemBasedDigest.getFromHex(hex3)));
}
long end = System.currentTimeMillis();
System.out.println("Algo: " + algo + " KeyLength: " + bmd5.length + " Time: " + (end - start));
}
}
} catch (NoSuchAlgorithmException e) {
fail(e.getMessage());
} catch (IOException e) {
fail(e.getMessage());
try {
for (DigestAlgo algo : DigestAlgo.values()) {
long start = System.currentTimeMillis();
byte[] bmd5 = null;
for (int i = 0; i < 10000; i++) {
FilesystemBasedDigest.setUseFastMd5(false);
FilesystemBasedDigest digest = new FilesystemBasedDigest(algo);
digest.Update(TESTPHRASEBYTES, 0, TESTPHRASEBYTES.length);
bmd5 = digest.Final();
String hex = FilesystemBasedDigest.getHex(bmd5);
assertTrue(algo + " Hex Not Equals",
FilesystemBasedDigest.digestEquals(hex, bmd5));
digest = new FilesystemBasedDigest(algo);
ByteBuf buf = Unpooled.wrappedBuffer(TESTPHRASEBYTES);
digest.Update(buf);
bmd5 = digest.Final();
hex = FilesystemBasedDigest.getHex(bmd5);
assertTrue(algo + " Hex Not Equals",
FilesystemBasedDigest.digestEquals(hex, bmd5));
FilesystemBasedDigest.setUseFastMd5(true);
FilesystemBasedDigest digest2 = new FilesystemBasedDigest(algo);
digest2.Update(TESTPHRASEBYTES, 0, TESTPHRASEBYTES.length);
byte[] bmd52 = digest2.Final();
String hex2 = FilesystemBasedDigest.getHex(bmd52);
assertTrue(algo + " Hex Not Equals",
FilesystemBasedDigest.digestEquals(hex2, bmd52));
assertTrue(algo + " FastMD5 vs MD5 Not Equals",
FilesystemBasedDigest.digestEquals(bmd52, bmd5));
FilesystemBasedDigest.setUseFastMd5(false);
buf = Unpooled.wrappedBuffer(TESTPHRASEBYTES);
byte[] bmd53 = FilesystemBasedDigest.getHash(buf, algo);
buf.release();
String hex3 = FilesystemBasedDigest.getHex(bmd53);
assertTrue(algo + " Hex Not Equals",
FilesystemBasedDigest.digestEquals(hex3, bmd53));
assertTrue(algo + " Through ByteBuf vs Direct Not Equals",
FilesystemBasedDigest.digestEquals(bmd53, bmd5));
assertTrue(algo + " FromHex Not Equals",
FilesystemBasedDigest.digestEquals(bmd53,
FilesystemBasedDigest
.getFromHex(hex3)));
}
long end = System.currentTimeMillis();
System.out.println(
"Algo: " + algo + " KeyLength: " + bmd5.length + " Time: " +
(end - start));
}
FilesystemBasedDigest.setUseFastMd5(false);
ByteBuf buf = Unpooled.wrappedBuffer(TESTPHRASEBYTES);
byte[] bmd5 = FilesystemBasedDigest.getHashMd5(buf);
byte[] bmd52 = FilesystemBasedDigest.getHash(buf, DigestAlgo.MD5);
assertTrue(DigestAlgo.MD5 + " Hex Not Equals",
FilesystemBasedDigest.digestEquals(bmd52, bmd5));
} catch (NoSuchAlgorithmException e) {
fail(e.getMessage());
} catch (IOException e) {
fail(e.getMessage());
}
}

@Test
public void testPasswdCryptString() {
for (int j = 0; j < 2; j++) {
long start = System.currentTimeMillis();
byte[] bmd5 = null;
for (int i = 0; i < 100000; i++) {
FilesystemBasedDigest.setUseFastMd5(false);
String crypt = FilesystemBasedDigest.passwdCrypt(TESTPHRASE);
byte[] bcrypt = FilesystemBasedDigest.passwdCrypt(TESTPHRASEBYTES);
bmd5 = bcrypt;
assertTrue("Password Hex Not Equals", FilesystemBasedDigest.digestEquals(crypt, bcrypt));
assertTrue("Password Not Equals", FilesystemBasedDigest.equalPasswd(TESTPHRASEBYTES, bcrypt));
assertTrue("Password Not Equals", FilesystemBasedDigest.equalPasswd(TESTPHRASE, crypt));
}
long end = System.currentTimeMillis();
System.out.println("Algo: CRYPT KeyLength: " + bmd5.length + " Time: " + (end - start));
@Test
public void testGetHashFileDigestAlgo() throws IOException {
File file = File.createTempFile("testHash", ".txt", new File("/tmp"));
FileWriter fileWriterBig = new FileWriter(file);
fileWriterBig.write(TESTPHRASE);
fileWriterBig.flush();
fileWriterBig.close();
try {
for (DigestAlgo algo : DigestAlgo.values()) {
long start = System.currentTimeMillis();
byte[] bmd5 = null;
for (int i = 0; i < 1000; i++) {
FilesystemBasedDigest.setUseFastMd5(false);
bmd5 = FilesystemBasedDigest.getHash(file, false, algo);
byte[] bmd52 = FilesystemBasedDigest.getHash(file, true, algo);
FileInputStream stream = new FileInputStream(file);
byte[] bmd53 = FilesystemBasedDigest.getHash(stream, algo);
stream.close();
assertTrue(algo + " Hex Not Equals",
FilesystemBasedDigest.digestEquals(bmd5, bmd52));
assertTrue(algo + " Hex Not Equals",
FilesystemBasedDigest.digestEquals(bmd5, bmd53));
FilesystemBasedDigest.setUseFastMd5(true);
bmd5 = FilesystemBasedDigest.getHash(file, false, algo);
bmd52 = FilesystemBasedDigest.getHash(file, true, algo);
stream = new FileInputStream(file);
bmd53 = FilesystemBasedDigest.getHash(stream, algo);
stream.close();
assertTrue(algo + " Hex Not Equals",
FilesystemBasedDigest.digestEquals(bmd5, bmd52));
assertTrue(algo + " Hex Not Equals",
FilesystemBasedDigest.digestEquals(bmd5, bmd53));
}
long end = System.currentTimeMillis();
System.out.println(
"Algo: " + algo + " KeyLength: " + bmd5.length + " Time: " +
(end - start));
}
} catch (IOException e) {
fail(e.getMessage());
} finally {
file.delete();
}
}

@Test
public void testPasswdCryptString() {
long start = System.currentTimeMillis();
byte[] bmd5 = null;
for (int i = 0; i < 100000; i++) {
FilesystemBasedDigest.setUseFastMd5(false);
String crypt = FilesystemBasedDigest.passwdCrypt(TESTPHRASE);
byte[] bcrypt = FilesystemBasedDigest.passwdCrypt(TESTPHRASEBYTES);
bmd5 = bcrypt;
assertTrue("Password Hex Not Equals",
FilesystemBasedDigest.digestEquals(crypt, bcrypt));
assertTrue("Password Not Equals",
FilesystemBasedDigest.equalPasswd(TESTPHRASEBYTES, bcrypt));
assertTrue("Password Not Equals",
FilesystemBasedDigest.equalPasswd(TESTPHRASE, crypt));
}
long end = System.currentTimeMillis();
System.out.println(
"Algo: CRYPT KeyLength: " + bmd5.length + " Time: " + (end - start));
}

@Test
public void testSpecificMD5() throws IOException {
FilesystemBasedDigest.setUseFastMd5(false);
assertFalse(FilesystemBasedDigest.isUseFastMd5());
FilesystemBasedDigest.initializeMd5(false);
assertFalse(FilesystemBasedDigest.isUseFastMd5());
assertEquals(DigestAlgo.MD5.getByteSize() * 2, DigestAlgo.MD5.getHexSize());

File file = File.createTempFile("testHash", ".txt", new File("/tmp"));
FileWriter fileWriterBig = new FileWriter(file);
fileWriterBig.write(TESTPHRASE);
fileWriterBig.flush();
fileWriterBig.close();
try {
DigestAlgo algo = DigestAlgo.MD5;
long start = System.currentTimeMillis();
byte[] bmd5 = null;
for (int i = 0; i < 1000; i++) {
FilesystemBasedDigest.setUseFastMd5(false);
byte[] bmd51 = FilesystemBasedDigest.getHashMd5Nio(file);
bmd5 = bmd51;
byte[] bmd52 = FilesystemBasedDigest.getHashMd5(file);


assertTrue(algo + " Hex Not Equals",
FilesystemBasedDigest.digestEquals(bmd51, bmd52));

FilesystemBasedDigest.setUseFastMd5(true);
bmd51 = FilesystemBasedDigest.getHashMd5Nio(file);
bmd52 = FilesystemBasedDigest.getHashMd5(file);
assertTrue(algo + " Hex Not Equals",
FilesystemBasedDigest.digestEquals(bmd51, bmd52));
assertTrue(algo + " Hex Not Equals",
FilesystemBasedDigest.digestEquals(bmd5, bmd52));
}
long end = System.currentTimeMillis();
System.out.println(
"Algo: " + algo + " KeyLength: " + bmd5.length + " Time: " +
(end - start));
} catch (IOException e) {
fail(e.getMessage());
} finally {
file.delete();
}
}
}
Loading