-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.go
112 lines (91 loc) · 2.68 KB
/
hash.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"bufio"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"hash"
"io"
"os"
)
var map_hash = make(map[string]bool)
var map_keys = make(map[uint32]string)
var append_hash = []string{}
var Reset = "\033[0m"
var Red = "\033[31m"
var Green = "\033[32m"
var Yellow = "\033[33m"
var Blue = "\033[34m"
var Magenta = "\033[35m"
func insertHashSimple(inputHash string) bool {
if map_hash[inputHash] {
return false // Already in the map
}
if theHolder.itr >= theHolder.itr_ref {
append_hash = append(append_hash, inputHash)
map_hash[inputHash] = true
theHolder.itr = 1
// logrus.Info(inputHash)
return true
}
theHolder.itr++
return false
}
type HashInfo struct {
Md5 string `json:"md5"`
Sha1 string `json:"sha1"`
Sha256 string `json:"sha256"`
Sha512 string `json:"sha512"`
}
func CalculateBasicHashes(rd io.Reader) HashInfo {
md5 := md5.New()
sha1 := sha1.New()
sha256 := sha256.New()
sha512 := sha512.New()
// For optimum speed, Getpagesize returns the underlying system's memory page size.
pagesize := os.Getpagesize()
// wraps the Reader object into a new buffered reader to read the files in chunks
// and buffering them for performance.
readers := bufio.NewReaderSize(rd, pagesize)
// creates a multiplexer Writer object that will duplicate all write
// operations when copying data from source into all different hashing algorithms
// at the same time
multiWriter := io.MultiWriter(md5, sha1, sha256, sha512)
// Using a buffered reader, this will write to the writer multiplexer
// so we only traverse through the file once, and can calculate all hashes
// in a single byte buffered scan pass.
//
_, err := io.Copy(multiWriter, readers)
if err != nil {
panic(err.Error())
}
var info HashInfo
info.Md5 = hex.EncodeToString(md5.Sum(nil))
info.Sha1 = hex.EncodeToString(sha1.Sum(nil))
info.Sha256 = hex.EncodeToString(sha256.Sum(nil))
info.Sha512 = hex.EncodeToString(sha512.Sum(nil))
return info
}
// HasherReader calculates the hash of a byte stream
// As an underlying io.Reader is read from, the hash is updated
type HasherReader struct {
hash hash.Hash
reader io.Reader
}
// NewHasherReader creates a new HasherReader from a provided io.Raeder
func NewHasherReader(r io.Reader) HasherReader {
hash := sha1.New()
reader := io.TeeReader(r, hash)
return HasherReader{hash, reader}
}
// Hash returns the hash value
// Ensure all contents of the underlying io.Reader have been read
func (h HasherReader) Hash() string {
return hex.EncodeToString(h.hash.Sum(nil))
}
// Read allows HasherReader to conform to io.Reader interface
func (h HasherReader) Read(p []byte) (n int, err error) {
return h.reader.Read(p)
}