-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.go
More file actions
79 lines (63 loc) · 1.07 KB
/
hash.go
File metadata and controls
79 lines (63 loc) · 1.07 KB
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
69
70
71
72
73
74
75
76
77
78
79
package mail
import (
"bytes"
"context"
"crypto/sha256"
"io"
"log/slog"
"github.com/mnako/letters"
)
func attachs(ctx context.Context, a []letters.AttachedFile) <-chan []byte {
out := make(chan []byte)
go func() {
defer close(out)
for _, a := range a {
select {
case <-ctx.Done():
case out <- a.Data:
}
}
}()
return out
}
func embeds(ctx context.Context, e []letters.InlineFile) <-chan []byte {
out := make(chan []byte)
go func() {
defer close(out)
for _, e := range e {
select {
case <-ctx.Done():
case out <- e.Data:
}
}
}()
return out
}
func hash(ctx context.Context, data <-chan []byte) <-chan []byte {
out := make(chan []byte)
go func() {
defer close(out)
for {
select {
case <-ctx.Done():
return
case d, ok := <-data:
if !ok {
return
}
h := sha256.New()
_, err := io.Copy(h, bytes.NewReader(d))
if err != nil {
slog.WarnContext(
ctx,
"Error hashing data",
"error", err.Error(),
)
continue
}
out <- h.Sum(nil)
}
}
}()
return out
}