diff --git a/.gitignore b/.gitignore index cfdc549..bc42b32 100644 --- a/.gitignore +++ b/.gitignore @@ -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 ################# @@ -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 + diff --git a/pom.xml b/pom.xml index b742df5..8936d38 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 WaarpDigest Waarp Digest - 3.0.6 + 3.1.0 This project allows support of Digest (MD5, SHA1, CRC) for Commons Waarp project. http://waarp.github.com/WaarpDigest 2009 @@ -97,7 +97,7 @@ 1.6 1.6 true - true + true diff --git a/src/main/java/org/waarp/common/digest/Version.java b/src/main/java/org/waarp/common/digest/Version.java index aabb39e..c2cf952 100644 --- a/src/main/java/org/waarp/common/digest/Version.java +++ b/src/main/java/org/waarp/common/digest/Version.java @@ -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(); } diff --git a/src/test/java/.gitkeep b/src/test/java/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/test/java/org/waarp/common/digest/FilesystemBasedDigestTest.java b/src/test/java/org/waarp/common/digest/FilesystemBasedDigestTest.java index 280e43d..122f748 100644 --- a/src/test/java/org/waarp/common/digest/FilesystemBasedDigestTest.java +++ b/src/test/java/org/waarp/common/digest/FilesystemBasedDigestTest.java @@ -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 . + ******************************************************************************/ + 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(); + } + } } diff --git a/src/test/java/org/waarp/common/digest/MD5Test.java b/src/test/java/org/waarp/common/digest/MD5Test.java new file mode 100644 index 0000000..2dd70fe --- /dev/null +++ b/src/test/java/org/waarp/common/digest/MD5Test.java @@ -0,0 +1,78 @@ +/******************************************************************************* + * 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 . + ******************************************************************************/ + +package org.waarp.common.digest; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import org.junit.Test; + +import java.io.UnsupportedEncodingException; + +import static org.junit.Assert.*; + +public class MD5Test { + + @Test + public void updateTest() throws UnsupportedEncodingException { + String totest = "MonTest"; + MD5 md5 = new MD5(totest); + assertArrayEquals(md5.Final(), MD5.asByte(md5.asHex())); + MD5 md52 = new MD5(); + md52.Update(totest); + assertTrue(MD5.hashesEqual(md5.Final(), md52.Final())); + MD5 md53 = new MD5(); + byte[] btotest = totest.getBytes(); + md53.Update(btotest, btotest.length); + assertTrue(MD5.hashesEqual(md5.Final(), md53.Final())); + MD5 md54 = new MD5(); + md54.Update(btotest); + assertTrue(MD5.hashesEqual(md5.Final(), md54.Final())); + MD5 md55 = new MD5(); + md55.Update(totest, "UTF-8"); + assertTrue(MD5.hashesEqual(md5.Final(), md55.Final())); + MD5 md56 = new MD5(); + for (int i = 0; i < btotest.length; i++) { + md56.Update(btotest[i]); + } + assertTrue(MD5.hashesEqual(md5.Final(), md56.Final())); + MD5 md57 = new MD5(); + for (int i = 0; i < btotest.length; i++) { + md57.Update((int) btotest[i]); + } + assertTrue(MD5.hashesEqual(md5.Final(), md57.Final())); + ByteBuf buf = Unpooled.wrappedBuffer(btotest); + MD5 md58 = new MD5(); + md58.Update(buf); + assertTrue(MD5.hashesEqual(md5.Final(), md58.Final())); + } + + @Test + public void passwordTest() { + String passwd = "totest"; + String pwd = MD5.passwdCrypt(passwd); + assertTrue(MD5.equalPasswd(passwd, pwd)); + byte[] bpwd = MD5.passwdCrypt(passwd.getBytes()); + assertTrue(MD5.equalPasswd(passwd.getBytes(), bpwd)); + assertTrue(MD5.hashesEqual(MD5.asByte(pwd), bpwd)); + } + + +} \ No newline at end of file