-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathuint64.go
68 lines (57 loc) · 1.66 KB
/
uint64.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
package hide
import (
"encoding/json"
"math/big"
)
// Uint64 is an alias of uint64 with obfuscating/deobfuscating json marshaller
type Uint64 uint64
// MarshalJSON satisfies json.Marshaller and transparently obfuscates the value
// using Default prime
func (i *Uint64) MarshalJSON() ([]byte, error) {
return json.Marshal(Uint64Obfuscate(uint64(*i), nil, nil))
}
// UnmarshalJSON satisfies json.Marshaller and transparently deobfuscates the
// value using inverse of Default prime
func (i *Uint64) UnmarshalJSON(data []byte) error {
var obf uint64
if err := json.Unmarshal(data, &obf); err != nil {
*i = Uint64(obf)
return err
}
*i = Uint64(Uint64Deobfuscate(obf, nil, nil))
return nil
}
// Uint64Obfuscate obfuscates uint64 provided as the 1st parameter using prime
// provided as the second one. If the provided prime is nil it will fall back
// to Default prime
func Uint64Obfuscate(val uint64, prime, xor *big.Int) uint64 {
if prime == nil {
prime = Default.uint64prime
}
bg := new(big.Int).SetUint64(val)
modularMultiplicativeInverse(bg, prime, uint64Max)
if xor == nil {
xor = Default.uint64xor
}
if xor != nil {
bg.Xor(bg, xor)
}
return bg.Uint64()
}
// Uint64Deobfuscate deobfuscates uint64 provided as the 1st parameter using
// inverse provided as the second one. If the provided inverse is nil it will
// fall back to Default inverse
func Uint64Deobfuscate(val uint64, inverse, xor *big.Int) uint64 {
if inverse == nil {
inverse = Default.uint64inverse
}
bg := new(big.Int).SetUint64(val)
if xor == nil {
xor = Default.uint64xor
}
if xor != nil {
bg.Xor(bg, xor)
}
modularMultiplicativeInverse(bg, inverse, uint64Max)
return bg.Uint64()
}