1/*
2Copyright 2013 The Perkeep Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8     http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17// Package hashutil contains misc hashing functions lacking homes elsewhere.
18package hashutil // import "perkeep.org/internal/hashutil"
19
20import (
21	"crypto/sha1"
22	"crypto/sha256"
23	"fmt"
24	"hash"
25	"io"
26
27	"perkeep.org/pkg/blob"
28)
29
30// SHA256Prefix computes the SHA-256 digest of data and returns
31// its first twenty lowercase hex digits.
32func SHA256Prefix(data []byte) string {
33	h := sha256.New()
34	h.Write(data)
35	return fmt.Sprintf("%x", h.Sum(nil))[:20]
36}
37
38// SHA1Prefix computes the SHA-1 digest of data and returns
39// its first twenty lowercase hex digits.
40func SHA1Prefix(data []byte) string {
41	h := sha1.New()
42	h.Write(data)
43	return fmt.Sprintf("%x", h.Sum(nil))[:20]
44}
45
46// TrackDigestReader is an io.Reader wrapper which records the digest of what it reads.
47type TrackDigestReader struct {
48	r io.Reader
49	h hash.Hash
50
51	// DoLegacySHA1 sets whether to also compute the legacy SHA-1 hash.
52	DoLegacySHA1 bool
53	s1           hash.Hash // optional legacy SHA-1 hash, for servers with old data
54}
55
56func NewTrackDigestReader(r io.Reader) *TrackDigestReader {
57	return &TrackDigestReader{r: r}
58}
59
60// Hash returns the current hash sum.
61func (t *TrackDigestReader) Hash() hash.Hash {
62	return t.h
63}
64
65// LegacySHA1Hash returns the current legacy SHA-1 hash sum.
66func (t *TrackDigestReader) LegacySHA1Hash() hash.Hash {
67	return t.s1
68}
69
70func (t *TrackDigestReader) Read(p []byte) (n int, err error) {
71	n, err = t.r.Read(p)
72	if t.h == nil {
73		// TODO(mpl): maybe let the constructor take a Hash, and then no need to depend on blob pkg.
74		t.h = blob.NewHash()
75	}
76	t.h.Write(p[:n])
77
78	if t.DoLegacySHA1 {
79		if t.s1 == nil {
80			t.s1 = sha1.New()
81		}
82		t.s1.Write(p[:n])
83	}
84	return n, err
85}
86