-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpseudocrypt.go
91 lines (84 loc) · 2.31 KB
/
pseudocrypt.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
package pseudocrypt
import (
"math"
"math/big"
"strings"
)
type PseudoCrypt struct {
Chars string
Primes []int64
ModMulInv []int64
}
func Create () *PseudoCrypt {
return &PseudoCrypt {
/* Base 62 (ASCII ONLY) */
Chars: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
/* Next prime greater than 62 ^ n / 1.618033988749894848 */
Primes: []int64{
1,
41,
2377,
147299,
9132313,
566201239,
35104476161,
2176477521929,
134941606358731,
8366379594239857,
518715534842869223,
},
/* Modular multiplicative inverse */
ModMulInv: []int64{
1,
59,
1677,
187507,
5952585,
643566407,
22071637057,
294289236153,
88879354792675,
7275288500431249,
280042546585394647,
},
}
}
func (ps *PseudoCrypt) ToBase(n int64) (key string) {
base := int64(len(ps.Chars))
for n > 0 {
mod := n % base
key = ps.Chars[mod:mod+1] + key
n = n / base
}
return key
}
func (ps *PseudoCrypt) FromBase(key string) (n int64) {
base := int64(len(ps.Chars))
for i := int64(0) ; len(key) > 0 ; i++ {
c := key[len(key)-1:]
key = key[:len(key)-1]
dec := int64(strings.Index(ps.Chars, c))
n += dec * int64(math.Pow(float64(base), float64(i)))
}
return n
}
func (ps *PseudoCrypt) Hash(n int64, length int) (hash string) {
base := int64(len(ps.Chars))
ceil := int64(math.Pow(float64(base), float64(length)))
prime := ps.Primes[length]
dec := big.NewInt(0).Mod(big.NewInt(0).Mul(big.NewInt(n), big.NewInt(prime)), big.NewInt(ceil))
hash = ps.ToBase(dec.Int64());
if len(hash) < length {
hash = strings.Repeat("0", length-len(hash)) + hash;
}
return hash
}
func (ps *PseudoCrypt) Unhash(key string) (n int64) {
length := len(key)
base := int64(len(ps.Chars))
ceil := int64(math.Pow(float64(base), float64(length)))
mmi := ps.ModMulInv[length]
n = ps.FromBase(key)
dec := big.NewInt(0).Mod(big.NewInt(0).Mul(big.NewInt(n), big.NewInt(mmi)), big.NewInt(ceil))
return dec.Int64()
}