-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSha512nDigestPreImageAttackTest.groovy
More file actions
62 lines (54 loc) · 1.48 KB
/
Copy pathSha512nDigestPreImageAttackTest.groovy
File metadata and controls
62 lines (54 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.ndpar.utils.crypto
import org.junit.Test
class Sha512nDigestPreImageAttackTest {
/**
* 22 ms
*/
@Test
void pre_image_attack_sha_512_8() {
preImageAttack(8, 'A9')
def digest = new Sha512nDigest(8)
assert digest.digest('200') == 'A9'
}
/**
* 633 ms
*/
@Test
void pre_image_attack_sha_512_16() {
// preImageAttack(16, '3D4B')
def digest = new Sha512nDigest(16)
assert digest.digest('91199') == '3D4B'
}
/**
* 2 min (118651 ms)
*/
@Test
void pre_image_attack_sha_512_24() {
// preImageAttack(24, '3A7F27')
def digest = new Sha512nDigest(24)
assert digest.digest('10462227') == '3A7F27'
}
/**
* 3 hours (10063815 ms)
*/
@Test
void pre_image_attack_sha_512_32() {
// preImageAttack(32, 'C3C0357C')
def digest = new Sha512nDigest(32)
assert digest.digest('2872293553') == 'C3C0357C'
}
private preImageAttack(int bitOutputSize, String targetHash) {
def time = Timer.time { println(findPreImage(bitOutputSize, targetHash)) }
println "${bitOutputSize}bit: ${time}ms"
}
private findPreImage(int bitOutputSize, String targetHash) {
def digest = new Sha512nDigest(bitOutputSize)
BigInteger i = 0
for (; digest.digest(i.toString()) != targetHash; i++) {
if (i % 10_000_000 == 0) {
println i
}
}
i
}
}