Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 745b2ed

Browse files
committed
generate default hash seed for siphash
1 parent 93d6249 commit 745b2ed

File tree

5 files changed

+55
-4
lines changed

5 files changed

+55
-4
lines changed

‎build.gradle‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ tasks.withType(JavaCompile) { options.encoding = "UTF-8" }
2626

2727
group = 'com.github.myibu'
2828
archivesBaseName = "algorithm-java"
29-
version = "0.0.1"
29+
version = "0.0.1a"
3030

3131
repositories {
3232
mavenCentral()

‎readme.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Reference to: [SHA256.pdf](./docs/SHA256.pdf)
1616
<dependency>
1717
<groupId>com.github.myibu</groupId>
1818
<artifactId>algorithm-java</artifactId>
19-
<version>0.0.1</version>
19+
<version>0.0.1a</version>
2020
</dependency>
2121
```
2222

‎src/main/java/com/github/myibu/algorithm/hash/SipHash.java‎

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,53 @@
11
package com.github.myibu.algorithm.hash;
22

3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.IOException;
6+
import java.lang.management.ManagementFactory;
7+
import java.nio.file.Paths;
8+
39
/**
410
* SipHash 2-4 algorithm
511
* @author myibu
612
* Created on 2021年9月8日
713
*/
814
public class SipHash {
915
protected byte[] hashSeed;
10-
privatefinal int c;
11-
privatefinal int d;
16+
protected int c;
17+
protected int d;
1218
protected static final int DEFAULT_SEED_SIZE = 128 / 8;
19+
private static final String LINUX_RANDOM_FILE = "/dev/urandom";
20+
21+
public SipHash() {
22+
this.hashSeed = generateHashSeed();
23+
this.c = 2;
24+
this.d = 4;
25+
}
26+
27+
private byte[] generateHashSeed() {
28+
byte[] seed = new byte[DEFAULT_SEED_SIZE];
29+
boolean seedInitialized = false;
30+
File randomFile = Paths.get(LINUX_RANDOM_FILE).toFile();
31+
if (randomFile.exists() && randomFile.canRead()) {
32+
try {
33+
FileInputStream fis = new FileInputStream(randomFile);
34+
if (fis.read(seed, 0, seed.length) == DEFAULT_SEED_SIZE) {
35+
seedInitialized = true;
36+
}
37+
} catch (IOException e) {
38+
39+
}
40+
}
41+
if (!seedInitialized) {
42+
for (int j = 0; j < seed.length; j++) {
43+
long sec = System.currentTimeMillis() / 1000;
44+
long usec = System.nanoTime();
45+
long pid = Integer.parseInt(ManagementFactory.getRuntimeMXBean().getName().split("@")[0]);
46+
seed[j] = (byte)(sec ^ usec ^ pid);
47+
}
48+
}
49+
return seed;
50+
}
1351

1452
public SipHash(byte[] seed) {
1553
this(seed, 2, 4);

‎src/main/java/com/github/myibu/algorithm/hash/SipHash13.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
* Created on 2021年9月8日
77
*/
88
public class SipHash13 extends SipHash {
9+
public SipHash13() {
10+
super();
11+
this.c = 1;
12+
this.d = 3;
13+
}
14+
915
public SipHash13(byte[] seed) {
1016
super(seed, 1, 3);
1117
}

‎src/test/java/com/github/myibu/algorithm/AlgorithmTest.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ public void testSHA256() throws Exception {
1919
ByteOperator.byteArrayToHexString(bs));
2020
}
2121

22+
@Test
23+
public void testDefaultSipHash() throws Exception {
24+
SipHash sipHash = new SipHash();
25+
long hashRes = sipHash.hash("12345678");
26+
System.out.println(Long.toHexString(hashRes));
27+
}
28+
2229
@Test
2330
public void testSipHash() throws Exception {
2431
byte[] hashSeed = new byte[]{

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /