crypto

package
v0.0.0-...-ff244ab Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2021 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidHash                 = errors.New("Invalid hash format")
	ErrMismatchedHashAndPassphrase = errors.New("hash and password are different")
	ErrNoUpdateNeeded              = errors.New("hash already has correct parameters")
)

Errors

View Source
var SigningMethod = jwt.SigningMethodHS512

SigningMethod is the algorithm choosed for signing JWT. Currently, it is HMAC-SHA-512

Functions

func Base64Decode

func Base64Decode(value []byte) ([]byte, error)

Base64Decode decodes a value using base64.

func Base64Encode

func Base64Encode(value []byte) []byte

Base64Encode encodes a value using base64.

func CompareHashAndPassphrase

func CompareHashAndPassphrase(hash []byte, passphrase []byte) (needUpdate bool, err error)

CompareHashAndPassphrase compares a derived key with the possible cleartext equivalent. The parameters used in the provided derived key are used. The comparison performed by this function is constant-time.

It returns an error if the derived keys do not match. It also returns a needUpdate boolean indicating whether or not the passphrase hash has outdated parameters and should be recomputed.

func DecodeAuthMessage

func DecodeAuthMessage(c MACConfig, key, enc, additionalData []byte) ([]byte, error)

DecodeAuthMessage verifies a message authentified with message authentication code and returns the message value algon with the issued time of the message.

func EncodeAuthMessage

func EncodeAuthMessage(c MACConfig, key, value, additionalData []byte) ([]byte, error)

EncodeAuthMessage associates the given value with a message authentication code for integrity and authenticity.

If the value, when base64 encoded with a fixed size header is longer than the configured maximum length, it will panic.

Message format (name prefix is in MAC but removed from message):

<---------------- MAC input ---------------->
                         <---------- Message ---------->
| name | additional data |    time |  value  |     hmac |
| ---- |       ---       | 8 bytes |   ---   | 32 bytes |

func GenerateFromPassphrase

func GenerateFromPassphrase(passphrase []byte) ([]byte, error)

GenerateFromPassphrase returns the derived key of the passphrase using the parameters provided. The parameters are prepended to the derived key and separated by the "$" character (0x24). If the parameters provided are less than the minimum acceptable values, an error will be returned.

func GenerateRandomBytes

func GenerateRandomBytes(n int) []byte

GenerateRandomBytes returns securely generated random bytes. It will return an error if the system's secure random number generator fails to function correctly, in which case the caller should not continue.

func GenerateRandomString

func GenerateRandomString(n int) string

GenerateRandomString generates a secure random string of length N

func NewJWT

func NewJWT(secret []byte, claims jwt.Claims) (string, error)

NewJWT creates a JWT token with the given claims, and signs it with the secret

func ParseJWT

func ParseJWT(tokenString string, keyFunc jwt.Keyfunc, claims jwt.Claims) error

ParseJWT parses a string and checkes that is a valid JSON Web Token

func Timestamp

func Timestamp() int64

Timestamp returns the current timestamp, in seconds.

Types

type MACConfig

type MACConfig struct {
	Name   string
	MaxAge time.Duration
	MaxLen int
}

MACConfig contains all the options to encode or decode a message along with a proof of integrity and authenticity.

Key is the secret used for the HMAC key. It should contain at least 16 bytes and should be generated by a PRNG.

Name is an optional message name that won't be contained in the MACed messaged itself but will be MACed against.

Jump to

Keyboard shortcuts

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