Documentation
¶
Overview ¶
Package integrity parses Subresource Integrity metadata and verifies streams against cryptographic digests.
It supports SHA-256, SHA-384, and SHA-512 without depending on HTTP, storage, or package-manager types. MD5 and SHA-1 are supported for verification only, to check checksums that registries already publish in those algorithms. They are never valid SRI and must not be chosen for new integrity metadata.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Algorithm ¶
type Algorithm uint8
Algorithm identifies a supported digest algorithm.
const ( // SHA256 identifies SHA-256 digests. SHA256 Algorithm = iota // SHA384 identifies SHA-384 digests. SHA384 // SHA512 identifies SHA-512 digests. SHA512 // MD5 identifies MD5 digests. It is a compatibility check for checksums a // publisher already supplied and is not a Subresource Integrity // algorithm. Do not choose it for new integrity metadata. MD5 // SHA1 identifies SHA-1 digests. It is a compatibility check for // checksums a publisher already supplied and is not a Subresource // Integrity algorithm. Do not choose it for new integrity metadata. SHA1 )
type Digest ¶
type Digest struct {
// contains filtered or unexported fields
}
Digest is an immutable algorithm and digest-byte pair.
Use ParseHex, ParseChecksum or ParseSRI to construct a Digest. Bytes returns a copy of the stored bytes.
func ParseChecksum ¶ added in v0.2.0
ParseChecksum parses a single algorithm-prefixed hexadecimal checksum, such as "sha256-<hex>" or "sha1:<hex>", the form many registries publish instead of SRI. It accepts every supported algorithm, MD5 and SHA-1 included, and surrounding whitespace. Verify the result with Result.VerifyDigest.
func ParseHex ¶
ParseHex parses a hexadecimal digest for algorithm. It accepts every supported algorithm, MD5 and SHA-1 included, for callers that already know which algorithm a registry's checksum uses.
func (Digest) Checksum ¶ added in v0.2.0
Checksum returns the canonical algorithm-prefixed hexadecimal encoding, which ParseChecksum accepts.
func (Digest) Equal ¶
Equal reports whether two digests use the same algorithm and contain the same raw bytes. The byte comparison takes constant time for equal-length values.
type ErrIncomplete ¶
type ErrIncomplete struct{}
ErrIncomplete reports that verification was requested before the reader observed EOF.
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader calculates requested digests as bytes pass through it. Bytes remain unverified until a completed Result passes Verify.
Example ¶
package main
import (
"fmt"
"io"
"strings"
"github.com/git-pkgs/integrity"
)
func main() {
expected, err := integrity.ParseSRI("sha256-LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ=")
if err != nil {
panic(err)
}
reader, err := integrity.NewReader(strings.NewReader("hello"), integrity.SHA256)
if err != nil {
panic(err)
}
if _, err := io.Copy(io.Discard, reader); err != nil {
panic(err)
}
result := reader.Result()
fmt.Println(result.Bytes, result.Complete)
fmt.Println(result.Verify(expected) == nil)
}
Output: 5 true true
type Result ¶
Result is a snapshot of a Reader's calculated digests and progress.
func (Result) Verify ¶
Verify applies the W3C SRI matching rule to a completed result. Only the strongest algorithm in expected is considered, and any digest using that algorithm may match. expected must hold only SRI algorithms; use VerifyDigest for an MD5 or SHA-1 checksum.
func (Result) VerifyDigest ¶ added in v0.2.0
VerifyDigest checks a completed result against one exact digest, such as a registry checksum from ParseChecksum or ParseHex. It accepts every supported algorithm, MD5 and SHA-1 included, and requires that the reader calculated that algorithm.
type SRI ¶
type SRI []Digest
SRI is a Subresource Integrity metadata list.
func ParseSRI ¶
ParseSRI parses a whitespace-separated Subresource Integrity metadata list. It accepts standard and URL-safe base64 with optional padding and ignores options, which SRI currently leaves undefined.
Example ¶
package main
import (
"fmt"
"github.com/git-pkgs/integrity"
)
func main() {
metadata, err := integrity.ParseSRI("sha256-LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ=")
if err != nil {
panic(err)
}
fmt.Println(metadata[0].Algorithm())
fmt.Println(metadata[0].Hex())
}
Output: sha256 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824