sri

package module
v0.0.0-...-9b3d78f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 8, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

README

sri

Integrity,a library for parsing, manipulating, serializing, generating, and verifying Subresource Integrity hashes.

Example

Parse a string as Integrity to convert it to a struct:

package test

import (
	"github.com/stretchr/testify/assert"
	"testing"
)

func TestParseIntegrity(t *testing.T) {
	integrity, err := ParseStr("sha1-deadbeef=")
	assert.Nil(t, err)
	assert.True(t, integrity.hashes[0].Eq(&Hash{
		algo:   AlgoSha1,
		digest: "deadbeef=",
	}))
}

func TestSriCheck(t *testing.T) {
	sri, err := NewIntegrityFromRawStr("hello world")
	assert.Nil(t, err)
	a, err := sri.Check([]byte("hello world"))
	assert.Nil(t, err)
	assert.Equal(t, AlgoSha256, a)
}

func TestMatch(t *testing.T) {
	sri1, _ := NewIntegrityFromRawStr("hello")
	sri2, _ := NewIntegrityFromRawStr("hello")
	sri3, _ := NewIntegrityFromRawStr("world")
	sri2 = sri2.Join(sri3)
	a, ok := sri1.Matches(sri2)
	assert.True(t, ok)
	assert.Equal(t, AlgoSha256, a)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidHasherSrc = errors.New("invalid input hasher source")

Functions

This section is empty.

Types

type Algo

type Algo int

Algo Valid algorithms for integrity strings. Sha1 and Xxh3 are special cases in this library--they're not allowed by the current SRI spec, but they're useful enough that having first-class support makes sense. They should also be completely harmless to have in your strings if you do use it in a browser context--they just won't be used.

const (
	AlgoInvalid Algo = iota
	AlgoSha1
	AlgoSha256
	AlgoSha384
	AlgoSha512
)

func (Algo) FromCryptoHash

func (a Algo) FromCryptoHash(ht crypto.Hash) Algo

func (Algo) String

func (a Algo) String() string

func (Algo) ToCryptoHash

func (a Algo) ToCryptoHash() crypto.Hash

func (Algo) ToHashBuilder

func (a Algo) ToHashBuilder() hash.Hash

type Checker

type Checker struct {
	// contains filtered or unexported fields
}

func NewChecker

func NewChecker(sri *Integrity) *Checker

func (*Checker) Input

func (c *Checker) Input(data []byte) *Checker

func (*Checker) Result

func (c *Checker) Result() (Algo, error)

type Hash

type Hash struct {
	// contains filtered or unexported fields
}

Hash Represents a single algorithm/digest pair. This is mostly internal, although users might interact with it directly on occasion.

func NewHash

func NewHash(str string) (*Hash, error)

func (*Hash) Cmp

func (h *Hash) Cmp(other *Hash) int

func (*Hash) Eq

func (h *Hash) Eq(other *Hash) bool

func (*Hash) String

func (h *Hash) String() string

type Hasher

type Hasher struct {
	// contains filtered or unexported fields
}

type Integrity

type Integrity struct {
	// contains filtered or unexported fields
}

func NewIntegrityFromRawStr

func NewIntegrityFromRawStr(src string) (*Integrity, error)

func ParseStr

func ParseStr(src string) (*Integrity, error)

func (*Integrity) Check

func (i *Integrity) Check(data []byte) (Algo, error)

func (*Integrity) Join

func (i *Integrity) Join(other *Integrity) *Integrity

Join together two Integrity instances. Hashes will be grouped and sorted by algorithm but otherwise kept in the same order.

func (*Integrity) Matches

func (i *Integrity) Matches(other *Integrity) (Algo, bool)

Compares self against a given SRI to see if there's a match. The deciding algorithm is determined by other.

func (*Integrity) PickAlgo

func (i *Integrity) PickAlgo() Algo

func (*Integrity) String

func (i *Integrity) String() string

type IntegrityOpts

type IntegrityOpts struct {
	// contains filtered or unexported fields
}

func New

func New() *IntegrityOpts

func (*IntegrityOpts) AddAlgo

func (o *IntegrityOpts) AddAlgo(algo Algo) *IntegrityOpts

func (*IntegrityOpts) AddHash

func (o *IntegrityOpts) AddHash(ch crypto.Hash) *IntegrityOpts

func (*IntegrityOpts) Input

func (o *IntegrityOpts) Input(data []byte) *IntegrityOpts

func (*IntegrityOpts) InputStr

func (o *IntegrityOpts) InputStr(data string) *IntegrityOpts

func (*IntegrityOpts) Result

func (o *IntegrityOpts) Result() (*Integrity, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL