Skip to content

Commit

Permalink
Use a build flag to disable resumable digests.
Browse files Browse the repository at this point in the history
Signed-off-by: Andy Goldstein <agoldste@redhat.com>
  • Loading branch information
Andy Goldstein committed Apr 16, 2015
1 parent 03274b6 commit c1ecabf
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 42 deletions.
58 changes: 16 additions & 42 deletions digester.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ package digest

import (
"crypto/sha256"
"fmt"
"hash"

"github.com/jlhawn/go-crypto" // For ResumableHash
_ "github.com/jlhawn/go-crypto/sha256" // For Resumable SHA256
_ "github.com/jlhawn/go-crypto/sha512" // For Resumable SHA384, SHA512
)

// Digester calculates the digest of written data. It is functionally
Expand Down Expand Up @@ -38,43 +33,22 @@ func (d *Digester) Digest() Digest {
return NewDigest(d.alg, d.Hash)
}

// ResumableDigester is a digester that can export its internal state and be
// restored from saved state.
type ResumableDigester struct {
alg string
crypto.ResumableHash
}

var resumableHashAlgs = map[string]crypto.Hash{
"sha256": crypto.SHA256,
"sha384": crypto.SHA384,
"sha512": crypto.SHA512,
}

// NewResumableDigester creates a new ResumableDigester with the given hashing
// algorithm.
func NewResumableDigester(alg string) (ResumableDigester, error) {
hash, supported := resumableHashAlgs[alg]
if !supported {
return ResumableDigester{}, fmt.Errorf("unsupported resumable hash algorithm: %s", alg)
}

return ResumableDigester{
alg: alg,
ResumableHash: hash.New(),
}, nil
}

// NewCanonicalResumableDigester creates a ResumableDigester using the default
// digest algorithm.
func NewCanonicalResumableDigester() ResumableDigester {
return ResumableDigester{
alg: "sha256",
ResumableHash: crypto.SHA256.New(),
}
// ResumableHash is the common interface implemented by all resumable hash
// functions.
type ResumableHash interface {
// ResumableHash is a superset of hash.Hash
hash.Hash
// Len returns the number of bytes written to the Hash so far.
Len() uint64
// State returns a snapshot of the state of the Hash.
State() ([]byte, error)
// Restore resets the Hash to the given state.
Restore(state []byte) error
}

// Digest returns the current digest for this resumable digester.
func (d ResumableDigester) Digest() Digest {
return NewDigest(d.alg, d.ResumableHash)
// ResumableDigester is a digester that can export its internal state and be
// restored from saved state.
type ResumableDigester interface {
ResumableHash
Digest() Digest
}
52 changes: 52 additions & 0 deletions digester_resumable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// +build !noresumabledigest

package digest

import (
"fmt"

"github.com/jlhawn/go-crypto"
// For ResumableHash
_ "github.com/jlhawn/go-crypto/sha256" // For Resumable SHA256
_ "github.com/jlhawn/go-crypto/sha512" // For Resumable SHA384, SHA512
)

// resumableDigester implements ResumableDigester.
type resumableDigester struct {
alg string
crypto.ResumableHash
}

var resumableHashAlgs = map[string]crypto.Hash{
"sha256": crypto.SHA256,
"sha384": crypto.SHA384,
"sha512": crypto.SHA512,
}

// NewResumableDigester creates a new ResumableDigester with the given hashing
// algorithm.
func NewResumableDigester(alg string) (ResumableDigester, error) {
hash, supported := resumableHashAlgs[alg]
if !supported {
return resumableDigester{}, fmt.Errorf("unsupported resumable hash algorithm: %s", alg)
}

return resumableDigester{
alg: alg,
ResumableHash: hash.New(),
}, nil
}

// NewCanonicalResumableDigester creates a ResumableDigester using the default
// digest algorithm.
func NewCanonicalResumableDigester() ResumableDigester {
return resumableDigester{
alg: "sha256",
ResumableHash: crypto.SHA256.New(),
}
}

// Digest returns the current digest for this resumable digester.
func (d resumableDigester) Digest() Digest {
return NewDigest(d.alg, d.ResumableHash)
}

0 comments on commit c1ecabf

Please sign in to comment.