Skip to content

Commit

Permalink
Merge pull request #73 from Osiris-Team/master
Browse files Browse the repository at this point in the history
Added fastSHA512, fastSHA256 and fastSHA1 methods
  • Loading branch information
jdereg committed Sep 16, 2023
2 parents e2929f9 + ff84ab0 commit c608e75
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 33 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/build-maven.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven

name: Java CI with Maven

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up JDK 11
uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'temurin'
cache: maven
- name: Build with Maven
run: mvn -B package --file pom.xml
56 changes: 28 additions & 28 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,33 @@
<description>Java Utilities</description>
<url>https://github.com/jdereg/java-util</url>

<profiles>
<developers>
<developer>
<id>jdereg</id>
<name>John DeRegnaucourt</name>
<email>[email protected]</email>
</developer>
</developers>

<properties>
<version.slf4j>2.0.9</version.slf4j>
<version.logback>1.4.11</version.logback>
<version.junit>4.13.2</version.junit>
<version.json.io>4.14.1</version.json.io> <!-- testing only -->
<version.mockito>1.10.19</version.mockito> <!-- testing only -->
<version.plugin.compiler>3.11.0</version.plugin.compiler>
<version.plugin.gpg>3.1.0</version.plugin.gpg>
<version.plugin.javadoc>3.6.0</version.plugin.javadoc>
<version.plugin.nexus>1.6.13</version.plugin.nexus>
<version.plugin.surefire>3.1.2</version.plugin.surefire>
<version.plugin.source>3.3.0</version.plugin.source>
<version.plugin.felix.scr>1.26.4</version.plugin.felix.scr>
<version.plugin.felix.bundle>5.1.9</version.plugin.felix.bundle>
<version.agrona>1.19.2</version.agrona> <!-- testing only -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<profiles>

<profile>
<id>release-sign-artifacts</id>
Expand Down Expand Up @@ -58,32 +84,6 @@
<developerConnection>scm:git:[email protected]:jdereg/java-util.git</developerConnection>
</scm>

<developers>
<developer>
<id>jdereg</id>
<name>John DeRegnaucourt</name>
<email>[email protected]</email>
</developer>
</developers>

<properties>
<version.slf4j>2.0.9</version.slf4j>
<version.logback>1.4.11</version.logback>
<version.junit>4.13.2</version.junit>
<version.json.io>4.14.1</version.json.io> <!-- testing only -->
<version.mockito>1.10.19</version.mockito> <!-- testing only -->
<version.plugin.compiler>3.11.0</version.plugin.compiler>
<version.plugin.gpg>3.1.0</version.plugin.gpg>
<version.plugin.javadoc>3.6.0</version.plugin.javadoc>
<version.plugin.nexus>1.6.13</version.plugin.nexus>
<version.plugin.surefire>3.1.2</version.plugin.surefire>
<version.plugin.source>3.3.0</version.plugin.source>
<version.plugin.felix.scr>1.26.4</version.plugin.felix.scr>
<version.plugin.felix.bundle>5.1.9</version.plugin.felix.bundle>
<version.agrona>1.19.2</version.agrona> <!-- testing only -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
Expand All @@ -94,7 +94,7 @@
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>

<build>
<plugins>
<plugin>
Expand Down
61 changes: 57 additions & 4 deletions src/main/java/com/cedarsoftware/util/EncryptionUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private EncryptionUtilities()
}

/**
* Super-fast MD5 calculation from entire file. Uses FileChannel and
* Super-fast MD5 calculation from entire file. Uses FileChannel and
* direct ByteBuffer (internal JVM memory).
* @param file File that from which to compute the MD5
* @return String MD5 value.
Expand All @@ -50,18 +50,71 @@ public static String fastMD5(File file)
{
try (FileInputStream in = new FileInputStream(file))
{
return calculateMD5Hash(in.getChannel());
return calculateFileHash(in.getChannel(), getMD5Digest());
}
catch (IOException e)
{
return null;
}
}

/**
* Super-fast SHA-1 calculation from entire file. Uses FileChannel and
* direct ByteBuffer (internal JVM memory).
* @param file File that from which to compute the SHA-1
* @return String SHA-1 value.
*/
public static String fastSHA1(File file)
{
try (FileInputStream in = new FileInputStream(file))
{
return calculateFileHash(in.getChannel(), getSHA1Digest());
}
catch (IOException e)
{
return null;
}
}

/**
* Super-fast SHA-256 calculation from entire file. Uses FileChannel and
* direct ByteBuffer (internal JVM memory).
* @param file File that from which to compute the SHA-256
* @return String SHA-256 value.
*/
public static String fastSHA256(File file)
{
try (FileInputStream in = new FileInputStream(file))
{
return calculateFileHash(in.getChannel(), getSHA256Digest());
}
catch (IOException e)
{
return null;
}
}

/**
* Super-fast SHA-512 calculation from entire file. Uses FileChannel and
* direct ByteBuffer (internal JVM memory).
* @param file File that from which to compute the SHA-512
* @return String SHA-512 value.
*/
public static String fastSHA512(File file)
{
try (FileInputStream in = new FileInputStream(file))
{
return calculateFileHash(in.getChannel(), getSHA512Digest());
}
catch (IOException e)
{
return null;
}
}

public static String calculateMD5Hash(FileChannel ch) throws IOException
public static String calculateFileHash(FileChannel ch, MessageDigest d) throws IOException
{
ByteBuffer bb = ByteBuffer.allocateDirect(65536);
MessageDigest d = getMD5Digest();

int nRead;

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/cedarsoftware/util/TestEncryption.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void testFastMd50BytesReturned() throws Exception {
FileChannel f = mock(FileChannel.class);
when(f.read(any(ByteBuffer.class))).thenReturn(0).thenReturn(-1);

EncryptionUtilities.calculateMD5Hash(f);
EncryptionUtilities.calculateFileHash(f, EncryptionUtilities.getMD5Digest());
}


Expand Down

0 comments on commit c608e75

Please sign in to comment.