-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrehash.go
84 lines (77 loc) · 1.64 KB
/
rehash.go
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package rainbow
import (
"bufio"
"encoding/hex"
"io"
"strings"
)
var (
RainbowTable = map[string]string{}
)
const NotFound = "Not Found"
func ReadRainbowTable(rainbowTableReader io.Reader) error {
// レインボーテーブルの読み込み
scanner := bufio.NewScanner(rainbowTableReader)
for scanner.Scan() {
line := scanner.Text()
chain := strings.Split(line, " ")
if len(chain) != 2 {
break
}
RainbowTable[chain[0]] = chain[1]
}
return nil
}
func ReHash(hash []byte, H HashFunc, R ReductionFunc) []byte {
// チェーンの復元
tailChan := make(chan string, 4096)
for i := 0; i < ChainLength; i++ {
go func(i int) {
rx := R(ChainLength-i-1, hash)
for j := 0; j < i; j++ {
beforeHash := H(rx)
rx = R(ChainLength-i+j, beforeHash)
}
tailChan <- string(rx)
}(i)
}
// テーブルとチェーンの照合
answer := make(chan []byte)
startGoroutine := make(chan int, 1024)
endGoroutine := make(chan int, 1024)
egCount := 0
sgCount := 0
for {
select {
case tail := <-tailChan:
go func(tail string) {
startGoroutine <- 1
defer func() { endGoroutine <- 1 }()
head, ok := RainbowTable[tail]
if !ok {
return
}
Rx := []byte(head)
hexHash := hex.EncodeToString(hash)
var Hx []byte
for j := 0; j < ChainLength; j++ {
Hx = H(Rx)
if hex.EncodeToString(Hx) == hexHash {
answer <- Rx
}
Rx = R(j, Hx)
}
}(tail)
case ans := <-answer:
return ans
case n := <-startGoroutine:
sgCount += n
case n := <-endGoroutine:
egCount += n
default:
if sgCount == ChainLength && egCount == ChainLength {
return []byte(NotFound)
}
}
}
}