krypto

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2025 License: Apache-2.0 Imports: 19 Imported by: 0

README

Update README.md with installation and examples:

markdownCopy# beaver-kit/krypto

Cryptographic utilities for Go applications

Install

go get github.com/gobeaver/beaver-kit/krypto

Features

  • Argon2id password hashing
  • JWT handling
  • OTP generation
  • RSA encryption
  • Token generation

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Argon2idHashPassword

func Argon2idHashPassword(password string) (string, error)

Argon2idHashPassword generates an Argon2id hash from the provided password. It uses a cryptographically secure random salt and configurable parameters for iterations, memory, parallelism, and key length to generate the hash. The generated hash is encoded in a string format containing information about the hash parameters and the encoded hash itself.

Parameters:

password: The plaintext password to be hashed.

Returns:

string: The hashed password encoded in a string format containing hash parameters.
error: An error, if any, encountered during the hashing process.

func BcryptCheckPasswordHash

func BcryptCheckPasswordHash(password, hash string) bool

func BcryptHashPassword

func BcryptHashPassword(password string) (string, error)

BcryptHashPassword generates a bcrypt hash from the provided password using a cost factor of 12. It returns the hashed password as a string.

Parameters:

password: The plaintext password to be hashed.

Returns:

string: The hashed password.
error: An error, if any, encountered during the hashing process.

func GenerateJWTFile

func GenerateJWTFile(jwtClaims jwt.Claims, filePath string) (string, error)

GenerateJWTFile generates a JWT token using a private key

func GenerateOTP

func GenerateOTP(length int) string

GenerateOTP generates a random One-Time Password (OTP) of the specified length. It uses a cryptographically secure random number generator to ensure the randomness of the generated OTP.

Parameters:

length: The length of the OTP to be generated.

Returns:

string: The randomly generated OTP.

func GenerateRandomString

func GenerateRandomString(length int) string

GenerateRandomString generates a random string of the specified length. It utilizes a pseudo-random number generator seeded with the current time to ensure randomness in the generated string.

Parameters:

length: The length of the random string to be generated.

Returns:

string: The randomly generated string.

func GenerateSecureToken

func GenerateSecureToken(length int) (string, error)

GenerateSecureToken generates a secure token of the specified length. It utilizes the cryptographic randomness provided by the rand package to ensure the security and unpredictability of the generated token.

Parameters:

length: The length of the secure token to be generated.

Returns:

string: The randomly generated secure token in hexadecimal format.
error: An error, if any, encountered during the token generation process.

func GenerateToken64

func GenerateToken64() string

func HashSHA256

func HashSHA256(input string) string

HashSHA256 hashes the input string using SHA-256 algorithm.

Parameters:

input (string): The input string to be hashed.

Returns:

string: The hexadecimal representation of the hashed value.

func NewHs256AccessToken

func NewHs256AccessToken(claims UserClaims) (string, error)

func NewHs256RefreshToken

func NewHs256RefreshToken(claims jwt.StandardClaims) (string, error)

func ParseHs256RefreshToken

func ParseHs256RefreshToken(refreshToken string) *jwt.StandardClaims

func RetryGenerateSecureToken

func RetryGenerateSecureToken(length int, retries int) (string, error)

func ValidateJWT

func ValidateJWT(tokenString string) (jwt.MapClaims, error)

ValidateJWT validates a JWT token using a public key

func ValidateRSAKeyPair

func ValidateRSAKeyPair(keyPair PublicPrivatePair) (bool, error)

ValidateRSAKeyPair validates an RSA key pair by encrypting and decrypting a message. It returns true if the validation succeeds, indicating that the key pair is valid. Otherwise, it returns false.

Parameters:

keyPair: The RSA public and private keys encoded in PEM format.

Returns:

bool: A boolean indicating whether the key pair is valid.
error: An error, if any, encountered during the validation process.

func VerifySHA256

func VerifySHA256(input, hashedValue string) bool

VerifySHA256 verifies if the input matches the hashed value.

Parameters:

input (string): The input string to be verified.
hashedValue (string): The hashed value to be compared with the hashed input.

Returns:

bool: Returns true if the input matches the hashed value; otherwise, returns false.

Types

type GenerateJwt

type GenerateJwt interface {
	GenerateJwtFile(claims jwt.Claims, filePath string) (string, error)
}

type PublicPrivatePair

type PublicPrivatePair struct {
	PrivateKey string
	PublicKey  string
}

func GenerateRSAKeyPair

func GenerateRSAKeyPair() (PublicPrivatePair, error)

GenerateRSAKeyPair generates a pair of RSA public and private keys with a key size of 2048 bits. It returns the public and private keys encoded in PEM format.

Returns:

PublicPrivatePair: A struct containing the RSA public and private keys
error: An error, if any, encountered during the key pair generation process.

type UserClaims

type UserClaims struct {
	First string `json:"first"`
	Last  string `json:"last"`
	Token string `json:"token"`
	jwt.StandardClaims
}

func ParseHs256AccessToken

func ParseHs256AccessToken(accessToken string) (*UserClaims, error)

Jump to

Keyboard shortcuts

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