-
Notifications
You must be signed in to change notification settings - Fork 7
/
int32.go
68 lines (57 loc) · 1.66 KB
/
int32.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"
)
// Int32 is an alias of int32 with obfuscating/deobfuscating json marshaller
type Int32 int32
// MarshalJSON satisfies json.Marshaller and transparently obfuscates the value
// using Default prime
func (i *Int32) MarshalJSON() ([]byte, error) {
return json.Marshal(Int32Obfuscate(int32(*i), nil, nil))
}
// UnmarshalJSON satisfies json.Marshaller and transparently deobfuscates the
// value using inverse of Default prime
func (i *Int32) UnmarshalJSON(data []byte) error {
var obf int32
if err := json.Unmarshal(data, &obf); err != nil {
*i = Int32(obf)
return err
}
*i = Int32(Int32Deobfuscate(obf, nil, nil))
return nil
}
// Int32Obfuscate obfuscates int32 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 Int32Obfuscate(val int32, prime, xor *big.Int) int32 {
if prime == nil {
prime = Default.int32prime
}
bg := new(big.Int).SetInt64(int64(val))
modularMultiplicativeInverse(bg, prime, int32Max)
if xor == nil {
xor = Default.int32xor
}
if xor != nil {
bg.Xor(bg, xor)
}
return int32(bg.Int64())
}
// Int32Deobfuscate deobfuscates int32 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 Int32Deobfuscate(val int32, inverse, xor *big.Int) int32 {
if inverse == nil {
inverse = Default.int32inverse
}
bg := new(big.Int).SetInt64(int64(val))
if xor == nil {
xor = Default.int32xor
}
if xor != nil {
bg.Xor(bg, xor)
}
modularMultiplicativeInverse(bg, inverse, int32Max)
return int32(bg.Int64())
}