security

package
v1.14.10 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2026 License: GPL-3.0 Imports: 5 Imported by: 0

Documentation

Overview

Package security provides tools for secure data storage and transitions. Security allows to easily encode and decode data to base62 format, hash data and validate hash, create and parse JWT.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(encoded string) ([]byte, error)

Decode decodes encoded data, to get it's original value.

func Encode

func Encode(data []byte) string

Encode encodes data for security purpose. For example, to send via HTTP.

func GenerateJWT

func GenerateJWT(
	value any,
	secretKey string,
	ttl time.Duration,
	algorithm string,
	opts ...jwt.TokenOption,
) (string, error)

GenerateJWT creates a new Json Web Token, based on provided data.

value - any value to store in JWT payload;

secretKey - is a secret, on base of which will be checked, if JWT can be trusted;

ttl - time JWT is appropriate and after which will be expired;

algorithm - JWT hashing algorithm like HS256 and so on.

func Hash

func Hash(value string, hashCost int) (string, error)

Hash hashes data for security purpose. For example, not to store raw unprotected data on database.

func ParseJWT

func ParseJWT(tokenString, secretKey string, opts ...jwt.ParserOption) (any, error)

ParseJWT decodes a Json Web Token payload.

tokenString - a JWT, which will be parsed;

secretKey - is a secret, on base of which will be checked, if JWT can be trusted;.

func RawDecode added in v1.1.3

func RawDecode(encoded string) ([]byte, error)

RawDecode decodes raw encoded data, to get it's original value.

func RawEncode added in v1.1.3

func RawEncode(data []byte) string

RawEncode encodes data for security purpose without padding characters. For example, to send in s3.

func ValidateHash

func ValidateHash(value, hashedValue string) bool

ValidateHash checks if hashed data is equal to raw data.

Types

type Config

type Config struct {
	HashCost int
	JWT      JWTConfig
}

Config is common security config.

type InvalidJWTError

type InvalidJWTError struct {
	Message string
	BaseErr error
}

InvalidJWTError is an error, which represents, that JWT expired or something else went wrong via parsing it.

func (InvalidJWTError) Error

func (e InvalidJWTError) Error() string

func (InvalidJWTError) Unwrap added in v1.2.11

func (e InvalidJWTError) Unwrap() error

type JWTClaimsError

type JWTClaimsError struct {
	Message string
	BaseErr error
}

JWTClaimsError is an error, which represents, that failed to retrieve JWT payload.

func (JWTClaimsError) Error

func (e JWTClaimsError) Error() string

func (JWTClaimsError) Unwrap added in v1.2.11

func (e JWTClaimsError) Unwrap() error

type JWTConfig

type JWTConfig struct {
	SecretKey       string
	Algorithm       string
	RefreshTokenTTL time.Duration
	AccessTokenTTL  time.Duration
}

JWTConfig is a config for creating and parsing Json Web Tokens.

Jump to

Keyboard shortcuts

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