-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathuint32.go
68 lines (57 loc) · 1.69 KB
/
uint32.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"
)
// Uint32 is an alias of uint32 with obfuscating/deobfuscating json marshaller
type Uint32 uint32
// MarshalJSON satisfies json.Marshaller and transparently obfuscates the value
// using Default prime
func (i *Uint32) MarshalJSON() ([]byte, error) {
return json.Marshal(Uint32Obfuscate(uint32(*i), nil, nil))
}
// UnmarshalJSON satisfies json.Marshaller and transparently deobfuscates the
// value using inverse of Default prime
func (i *Uint32) UnmarshalJSON(data []byte) error {
var obf uint32
if err := json.Unmarshal(data, &obf); err != nil {
*i = Uint32(obf)
return err
}
*i = Uint32(Uint32Deobfuscate(obf, nil, nil))
return nil
}
// Uint32Obfuscate obfuscates uint32 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 Uint32Obfuscate(val uint32, prime, xor *big.Int) uint32 {
if prime == nil {
prime = Default.uint32prime
}
bg := new(big.Int).SetUint64(uint64(val))
modularMultiplicativeInverse(bg, prime, uint32Max)
if xor == nil {
xor = Default.uint32xor
}
if xor != nil {
bg.Xor(bg, xor)
}
return uint32(bg.Uint64())
}
// Uint32Deobfuscate deobfuscates uint32 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 Uint32Deobfuscate(val uint32, inverse, xor *big.Int) uint32 {
if inverse == nil {
inverse = Default.uint32inverse
}
bg := new(big.Int).SetUint64(uint64(val))
if xor == nil {
xor = Default.uint32xor
}
if xor != nil {
bg.Xor(bg, xor)
}
modularMultiplicativeInverse(bg, inverse, uint32Max)
return uint32(bg.Uint64())
}