-
Notifications
You must be signed in to change notification settings - Fork 38
/
salt.go
57 lines (46 loc) · 1.02 KB
/
salt.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
package util
import (
"io"
)
// SaltXOR xors bits of data with salt
// repeating salt if necessary.
func SaltXOR(data, salt []byte) []byte {
return SaltXOROffset(data, salt, 0)
}
// SaltXOROffset xors bits of data with salt starting from off byte
// repeating salt if necessary.
func SaltXOROffset(data, salt []byte, off int) (result []byte) {
result = make([]byte, len(data))
ls := len(salt)
if ls == 0 {
copy(result, data)
return
}
for i := range result {
result[i] = data[i] ^ salt[(i+off)%ls]
}
return
}
type saltWriter struct {
w io.Writer
off int
salt []byte
}
// NewSaltingWriter returns io.Writer instance that applies
// salt to written data and write the result to w.
func NewSaltingWriter(w io.Writer, salt []byte) io.Writer {
if len(salt) == 0 {
return w
}
return &saltWriter{
w: w,
salt: salt,
}
}
func (w *saltWriter) Write(data []byte) (int, error) {
if dataLen := len(data); dataLen > 0 {
data = SaltXOROffset(data, w.salt, w.off)
w.off += dataLen
}
return w.w.Write(data)
}