This repository was archived by the owner on Oct 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrc.go
100 lines (87 loc) · 2.09 KB
/
crc.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"fmt"
"strconv"
)
// crack 反推bilibili midhash, 返回-1就是没有找到
func crack(input string) string {
// Create the CRC32 table.
var crctable [256]uint32
for i := 0; i < 256; i++ {
crcreg := uint32(i)
for j := 0; j < 8; j++ {
if crcreg&1 != 0 {
crcreg = 0xEDB88320 ^ (crcreg >> 1)
} else {
crcreg >>= 1
}
}
crctable[i] = crcreg
}
// CRC32 calculation.
crc32 := func(s string) uint32 {
crcstart := uint32(0xFFFFFFFF)
for i := 0; i < len(s); i++ {
index := (crcstart ^ uint32(s[i])) & 255
crcstart = (crcstart >> 8) ^ crctable[index]
}
return crcstart
}
// Get last index used in the CRC32 table.
crc32LastIndex := func(s string) int {
var index uint32
crcstart := uint32(0xFFFFFFFF)
for i := 0; i < len(s); i++ {
index = (crcstart ^ uint32(s[i])) & 255
crcstart = (crcstart >> 8) ^ crctable[index]
}
return int(index)
}
// Get CRC index.
getCRCIndex := func(t int) int {
for i := 0; i < 256; i++ {
if crctable[i]>>24 == uint32(t) {
return i
}
}
return -1
}
// Deep check.
deepCheck := func(i int, index []int) (bool, string) {
hashcode := crc32(strconv.Itoa(i))
tc := int(hashcode&0xff) ^ index[2]
if tc < 48 || tc > 57 {
return false, ""
}
result := string(rune(tc - 48 + '0'))
hashcode = crctable[index[2]] ^ (hashcode >> 8)
tc = int(hashcode&0xff) ^ index[1]
if tc < 48 || tc > 57 {
return false, ""
}
result += string(rune(tc - 48 + '0'))
hashcode = crctable[index[1]] ^ (hashcode >> 8)
tc = int(hashcode&0xff) ^ index[0]
if tc < 48 || tc > 57 {
return false, ""
}
result += string(rune(tc - 48 + '0'))
return true, result
}
var index = make([]int, 4)
ht, _ := strconv.ParseInt(input, 16, 64)
ht ^= 0xFFFFFFFF
for i := 3; i >= 0; i-- {
index[3-i] = getCRCIndex(int(ht >> (i * 8)))
snum := crctable[index[3-i]]
ht ^= int64(snum >> ((3 - i) * 8))
}
for i := 0; i < 100000000; i++ {
if crc32LastIndex(strconv.Itoa(i)) == index[3] {
if valid, result := deepCheck(i, index); valid {
return fmt.Sprintf("%d%s", i, result)
}
}
}
return "-1"
}