Skip to content
Merged
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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ tasks.withType(JavaCompile) { options.encoding = "UTF-8" }

group = 'com.github.myibu'
archivesBaseName = "algorithm-java"
version = "0.0.1"
version = "0.0.1a"

repositories {
mavenCentral()
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Reference to: [SHA256.pdf](./docs/SHA256.pdf)
<dependency>
<groupId>com.github.myibu</groupId>
<artifactId>algorithm-java</artifactId>
<version>0.0.1</version>
<version>0.0.1a</version>
</dependency>
```

Expand Down
42 changes: 40 additions & 2 deletions src/main/java/com/github/myibu/algorithm/hash/SipHash.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,53 @@
package com.github.myibu.algorithm.hash;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.nio.file.Paths;

/**
* SipHash 2-4 algorithm
* @author myibu
* Created on 2021/9/8
*/
public class SipHash {
protected byte[] hashSeed;
private final int c;
private final int d;
protected int c;
protected int d;
protected static final int DEFAULT_SEED_SIZE = 128 / 8;
private static final String LINUX_RANDOM_FILE = "/dev/urandom";

public SipHash() {
this.hashSeed = generateHashSeed();
this.c = 2;
this.d = 4;
}

private byte[] generateHashSeed() {
byte[] seed = new byte[DEFAULT_SEED_SIZE];
boolean seedInitialized = false;
File randomFile = Paths.get(LINUX_RANDOM_FILE).toFile();
if (randomFile.exists() && randomFile.canRead()) {
try {
FileInputStream fis = new FileInputStream(randomFile);
if (fis.read(seed, 0, seed.length) == DEFAULT_SEED_SIZE) {
seedInitialized = true;
}
} catch (IOException e) {

}
}
if (!seedInitialized) {
for (int j = 0; j < seed.length; j++) {
long sec = System.currentTimeMillis() / 1000;
long usec = System.nanoTime();
long pid = Integer.parseInt(ManagementFactory.getRuntimeMXBean().getName().split("@")[0]);
seed[j] = (byte)(sec ^ usec ^ pid);
}
}
return seed;
}

public SipHash(byte[] seed) {
this(seed, 2, 4);
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/github/myibu/algorithm/hash/SipHash13.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
* Created on 2021/9/8
*/
public class SipHash13 extends SipHash {
public SipHash13() {
super();
this.c = 1;
this.d = 3;
}

public SipHash13(byte[] seed) {
super(seed, 1, 3);
}
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/com/github/myibu/algorithm/AlgorithmTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ public void testSHA256() throws Exception {
ByteOperator.byteArrayToHexString(bs));
}

@Test
public void testDefaultSipHash() throws Exception {
SipHash sipHash = new SipHash();
long hashRes = sipHash.hash("12345678");
System.out.println(Long.toHexString(hashRes));
}

@Test
public void testSipHash() throws Exception {
byte[] hashSeed = new byte[]{
Expand Down