-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaesf_reader.go
63 lines (51 loc) · 1.13 KB
/
aesf_reader.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
package aesf
import (
"bytes"
"crypto/cipher"
"hash"
"io"
"strconv"
)
type ReaderSizeError int
func (r ReaderSizeError) Error() string {
return "asef: invalid block size " + strconv.Itoa(int(r))
}
type aesfReader struct {
s cipher.Stream
r io.Reader
hash hash.Hash
authenticate []byte
}
func (r *aesfReader) Read(block []byte) (n int, err error) {
blockSize := len(block)
if blockSize-SignatureKeySize <= 0 {
return 0, io.EOF
}
n, err = r.r.Read(block)
if n == 0 || err == io.EOF {
return n, io.EOF
}
if err != nil {
return 0, io.ErrUnexpectedEOF
}
p := n - SignatureKeySize + len(r.authenticate)
cipher := make([]byte, 0)
cipher = append(cipher, r.authenticate...)
cipher = append(cipher, block...)
r.s.XORKeyStream(block[:p], cipher[:p])
r.hash.Write(block[:p])
r.authenticate = cipher[p : p+SignatureKeySize]
if n < blockSize {
return p, io.EOF
}
return p, nil
}
func (r *aesfReader) Close() error {
if !bytes.Equal(r.hash.Sum(nil)[:SignatureKeySize], r.authenticate) {
return ErrSignatureFail
}
if c, ok := r.r.(io.Closer); ok {
return c.Close()
}
return nil
}