This repository was archived by the owner on May 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhasher.go
80 lines (69 loc) · 2.02 KB
/
hasher.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
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2018 IBM Corporation
// Licensed under the Apache License, Version 2.0. See LICENSE file.
package hash
import (
"hash"
"io"
)
// AsyncHash is a wrapper for hash.Hash that operates asynchronously
// by using a buffering pipe to accept input and a channel to provide
// the final digest
type AsyncHash interface {
hash.Hash
io.Closer
Digest() Digest
Done() <-chan Digest
}
// // func hashCh(h hash.Hash) (io.WriteCloser, chan []byte) {
// // ch := make(chan []byte, 0)
// // r, w := io.Pipe()
// // go func() {
// // defer r.Close()
// // if _, err := io.Copy(h, r); err != nil {
// // log.Fatal(err)
// // }
// // ch <- h.Sum(nil)
// // }()
// // return w, ch
// // }
// // func multiCopyClose(r io.Reader, writers ...io.Writer) (int64, error) {
// // for _, mwc := range writers {
// // if c, isCloser := mwc.(io.Closer); isCloser {
// // defer c.Close()
// // }
// // }
// // w := io.MultiWriter(writers...)
// // return io.Copy(w, r)
// // }
// func asyncHashSum(r io.Reader, hasher hash.Hash) (io.Reader, <-chan error) {
// errChannel := make(chan error, 1)
// // if _, ok := r.(passthroughHasher); ok {
// // panic("nested passthrough hasher!")
// // }
// if r == nil {
// panic("nil reader? seriously?")
// }
// readerToHash, hashWriter := io.Pipe()
// wrapRead, wrapWrite := io.Pipe()
// // like TeeReader except we close the writer when we read io.EOF
// go func() {
// defer hashWriter.Close()
// defer wrapWrite.Close()
// mw := io.MultiWriter(hashWriter, wrapWrite)
// n, err := io.Copy(mw, r)
// if err != nil {
// log.Println("Bytes read in passthrough: ", n)
// log.Println("io error in passthrough:", err)
// } else {
// // log.Println("Bytes read in passthrough: ", n)
// }
// }()
// go func() {
// defer close(errChannel)
// _, err := io.Copy(hasher, readerToHash)
// errChannel <- err
// }()
// // we use this un-exported type wrapper to detect recursive wrapping
// passthroughReader := wrapRead
// return passthroughReader, errChannel
// }