Documentation ¶
Overview ¶
Package crypto collects common cryptographic components.
Hasher is currently the only component exposed by this package. A hasher can be created using a HasherType enum value (NewHasher), but it can also be created using NewDefaultHasher256/NewDefaultHasher512 or the constructor for the Hasher itself.
Each enumeration value also has a string version, which is used by the HashType in order to implement the TextMarshaler and TextUnmarshaler interfaces.
You can use the RegisterHasher function to register your own hash by giving a unique HashType enum value, string version and constructor. This will make the hash type a first-class citizen of this package.
You can also overwrite an existing hash function, by using a HashType enum value Already used in a prior registration, and in this case the str input parameter doesn't have to be given, as the existing string version will be used in that case.
Each Hasher supported by this package also has a standalone Sum function which can be used to create a checksum, without having to create a Hasher first, this is useful for scenarios where you only need to hash infrequently.
These non-processing components are used in the 0-stor client If you're looking for cryptographic components
Index ¶
- Constants
- func RegisterHasher(ht HashType, str string, hc HasherConstructor)
- func Sum256(data []byte) (hash []byte)
- func Sum512(data []byte) (hash []byte)
- func SumBlake2b256(data []byte) []byte
- func SumBlake2b512(data []byte) []byte
- func SumSHA256(data []byte) []byte
- func SumSHA512(data []byte) []byte
- type Blake2b256Hasher
- type Blake2b512Hasher
- type HashFunc
- type HashType
- type Hasher
- type HasherConstructor
- type SHA256Hasher
- type SHA512Hasher
Constants ¶
const ( // HashTypeBlake2b256 is the enum constant which identifies Blake2b-256, // a cryptographic hashing algorithm which produces a secure hash of 32 bytes. // This type is also the default HashType. HashTypeBlake2b256 HashType = iota // HashTypeBlake2b512 is the enum constant which identifies Blake2b-512, // a cryptographic hashing algorithm which produces a secure hash of 64 bytes. HashTypeBlake2b512 // HashTypeSHA256 is the enum constant which identifies SHA256, // a cryptographic hashing algorithm which produces a secure hash of 32 bytes. HashTypeSHA256 // HashTypeSHA512 is the enum constant which identifies SHA512, // a cryptographic hashing algorithm which produces a secure hash of 64 bytes. HashTypeSHA512 // DefaultHash256Type represents the default 256 bit // Hashing algorithm as promoted by this package. // // This package reserves the right to change the // default 256 bit hashing algorithm at any time, // but this constant will always be available and up to date. DefaultHash256Type = HashTypeBlake2b256 // DefaultHash512Type represents the default 512 bit // hashing algorithm as promoted by this package. // // This package reserves the right to change the // default 512 bit hashing algorithm at any time, // but this constant will always be available and up to date. DefaultHash512Type = HashTypeBlake2b512 // DefaultHashType represents the default // hashing algorithm as promoted by this package. // // For now it will be an alias for the default 256-bit hash type, // but this package reserves the right to change this, // should this be required for security reasons in the future. DefaultHashType = DefaultHash256Type // MaxStandardHashType defines the hasher type, // which has the greatest defined/used enum value. // When defining your custom HashType you can do so as follows: // // const ( // MyHashType = iota + processing.MaxStandardHashType + 1 // MyOtherHashType // // ... // ) // // The maximum allowed value of a custom hash type is 255, // due to the underlying uint8 type. MaxStandardHashType = HashTypeSHA512 )
Variables ¶
This section is empty.
Functions ¶
func RegisterHasher ¶
func RegisterHasher(ht HashType, str string, hc HasherConstructor)
RegisterHasher registers a new or overwrite an existing hash algorithm. The given str will be used in a case-insensitive manner, if the registered hash however overwrites an existing hash type, the str parameter will be ignored and instead the already used string version will be used. This is intended to be called from the init function in packages that implement hash functions.
func Sum256 ¶
Sum256 create and returns a hash, for and given some binary input data, using the default hashing algorithm, Blake2b_256.
func Sum512 ¶
Sum512 create and returns a hash, for and given some binary input data, using the default hashing algorithm, Blake2b_512.
func SumBlake2b256 ¶
SumBlake2b256 creates and returns a hash, for and given some binary input data, using the third-party blake2b-256 algorithm.
func SumBlake2b512 ¶
SumBlake2b512 creates and returns a hash, for and given some binary input data, using the third-party blake2b-512 algorithm.
Types ¶
type Blake2b256Hasher ¶
type Blake2b256Hasher struct {
// contains filtered or unexported fields
}
Blake2b256Hasher defines a crypto-hasher, using the third-party blake2b-256 algorithm. It can be used to create a hash, given some binary input data.
func NewBlake2b256Hasher ¶
func NewBlake2b256Hasher(key []byte) (*Blake2b256Hasher, error)
NewBlake2b256Hasher creates a new hasher, using the Blake2b (32 bytes output) algorithm.
func (Blake2b256Hasher) HashBytes ¶
func (hasher Blake2b256Hasher) HashBytes(data []byte) []byte
HashBytes implements Hasher.HashBytes
type Blake2b512Hasher ¶
type Blake2b512Hasher struct {
// contains filtered or unexported fields
}
Blake2b512Hasher defines a crypto-hasher, using the third-party blake2b-512 algorithm. It can be used to create a hash, given some binary input data.
func NewBlake2b512Hasher ¶
func NewBlake2b512Hasher(key []byte) (*Blake2b512Hasher, error)
NewBlake2b512Hasher creates a new hasher, using the Blake2b (64 bytes output) algorithm.
func (Blake2b512Hasher) HashBytes ¶
func (hasher Blake2b512Hasher) HashBytes(data []byte) []byte
HashBytes implements Hasher.HashBytes
type HashType ¶
type HashType uint8
HashType represents a cryptographic hashing algorithm.
func (HashType) MarshalText ¶
MarshalText implements encoding.TextMarshaler.MarshalText
func (*HashType) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.UnmarshalText
type Hasher ¶
type Hasher interface { // HashBytes creates a secure hash, // given some input data. HashBytes(data []byte) (hash []byte) }
Hasher defines the interface of a crypto-hasher, which can be used to create a hash, given some binary input data.
Hasher is /NEVER/ thread-safe, and should only ever be used on /ONE/ goroutine at a time. If you wish to hash on multiple goroutines, you'll have to create one Hasher per goroutine.
func NewDefaultHasher256 ¶
NewDefaultHasher256 returns a new instance of the default hasher type.
Key is an optional private key to add authentication to the output, when the key is not given the hasher will produce cryptographically secure checksums, without any proof of ownership.
The default hasher is currently Blake2b_256, which produces checksums, or signatures if a key is given, of 32 bytes.
This package reserves the right to change the default 256 bit hashing algorithm at any time, but this constructor will always be available and up to date.
func NewDefaultHasher512 ¶
NewDefaultHasher512 returns a new instance of the default hasher type.
Key is an optional private key to add authentication to the output, when the key is not given the hasher will produce cryptographically secure checksums, without any proof of ownership.
The default hasher is currently Blake2b_512, which produces checksums, or signatures if a key is given, of 64 bytes.
This package reserves the right to change the default 512 bit hashing algorithm at any time, but this constructor will always be available and up to date.
func NewHasher ¶
NewHasher returns a new instance for the given hasher type. If the hasher type is invalid, an error is returned.
Key is an optional private key to add authentication to the output, when the key is not given the hasher will produce cryptographically secure checksums, without any proof of ownership.
type HasherConstructor ¶
HasherConstructor defines a function which can be used to create a hasher. The key parameter is an optional private key which can be used, to create signatures as to provide authentication.
type SHA256Hasher ¶
type SHA256Hasher struct {
// contains filtered or unexported fields
}
SHA256Hasher defines a crypto-hasher, using the std SHA256 algorithm. It can be used to create a hash, given some binary input data.
func NewSHA256Hasher ¶
func NewSHA256Hasher(key []byte) (*SHA256Hasher, error)
NewSHA256Hasher creates a new hasher, using the SHA256 (32 bytes output) algorithm.
Key is an optional private key to add authentication to the output, when the key is not given the hasher will produce cryptographically secure checksums, without any proof of ownership.
func (SHA256Hasher) HashBytes ¶
func (hasher SHA256Hasher) HashBytes(data []byte) []byte
HashBytes implements Hasher.HashBytes
type SHA512Hasher ¶
type SHA512Hasher struct {
// contains filtered or unexported fields
}
SHA512Hasher defines a crypto-hasher, using the std SHA512 algorithm. It can be used to create a hash, given some binary input data.
func NewSHA512Hasher ¶
func NewSHA512Hasher(key []byte) (*SHA512Hasher, error)
NewSHA512Hasher creates a new hasher, using the SHA512 (64 bytes output) algorithm.
Key is an optional private key to add authentication to the output, when the key is not given the hasher will produce cryptographically secure checksums, without any proof of ownership.
func (SHA512Hasher) HashBytes ¶
func (hasher SHA512Hasher) HashBytes(data []byte) []byte
HashBytes implements Hasher.HashBytes