File: checksum.go

package info (click to toggle)
aptly 1.6.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 49,928 kB
  • sloc: python: 10,398; sh: 252; makefile: 184
file content (112 lines) | stat: -rw-r--r-- 2,442 bytes parent folder | download | duplicates (2)
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 utils

import (
	"crypto/md5"
	"crypto/sha1"
	"crypto/sha256"
	"crypto/sha512"
	"fmt"
	"hash"
	"io"
	"os"
)

// MD5ChecksumForFile computes just the MD5 hash and not all the others
func MD5ChecksumForFile(path string) (string, error) {
	file, err := os.Open(path)
	if err != nil {
		return "", err
	}
	defer func() {
		_ = file.Close()
	}()

	hash := md5.New()
	_, err = io.Copy(hash, file)
	if err != nil {
		return "", err
	}

	return fmt.Sprintf("%x", hash.Sum(nil)), nil
}

// ChecksumInfo represents checksums for a single file
type ChecksumInfo struct {
	Size   int64
	MD5    string
	SHA1   string
	SHA256 string
	SHA512 string
}

// Complete checks if all the checksums are present
func (cksum *ChecksumInfo) Complete() bool {
	return cksum.MD5 != "" && cksum.SHA1 != "" && cksum.SHA256 != "" && cksum.SHA512 != ""
}

// ChecksumsForReader generates size, MD5, SHA1 & SHA256 checksums for the given
// io.Reader
func ChecksumsForReader(rd io.Reader) (ChecksumInfo, error) {
	w := NewChecksumWriter()

	_, err := io.Copy(w, rd)
	if err != nil {
		return ChecksumInfo{}, err
	}

	return w.Sum(), nil
}

// ChecksumsForFile generates size, MD5, SHA1 & SHA256 checksums for the file at
// the given path
func ChecksumsForFile(path string) (ChecksumInfo, error) {
	file, err := os.Open(path)
	if err != nil {
		return ChecksumInfo{}, err
	}
	defer func() {
		_ = file.Close()
	}()

	return ChecksumsForReader(file)
}

// ChecksumWriter is a writer that does checksum calculation on the fly passing data
// to real writer
type ChecksumWriter struct {
	sum    ChecksumInfo
	hashes []hash.Hash
}

// Interface check
var (
	_ io.Writer = &ChecksumWriter{}
)

// NewChecksumWriter creates checksum calculator for given writer w
func NewChecksumWriter() *ChecksumWriter {
	return &ChecksumWriter{
		hashes: []hash.Hash{md5.New(), sha1.New(), sha256.New(), sha512.New()},
	}
}

// Write implememnts pass-through writing with checksum calculation on the fly
func (c *ChecksumWriter) Write(p []byte) (n int, err error) {
	c.sum.Size += int64(len(p))

	for _, h := range c.hashes {
		h.Write(p)
	}

	return len(p), nil
}

// Sum returns caculated ChecksumInfo
func (c *ChecksumWriter) Sum() ChecksumInfo {
	c.sum.MD5 = fmt.Sprintf("%x", c.hashes[0].Sum(nil))
	c.sum.SHA1 = fmt.Sprintf("%x", c.hashes[1].Sum(nil))
	c.sum.SHA256 = fmt.Sprintf("%x", c.hashes[2].Sum(nil))
	c.sum.SHA512 = fmt.Sprintf("%x", c.hashes[3].Sum(nil))

	return c.sum
}