encryption

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2025 License: MIT Imports: 21 Imported by: 2

Documentation

Index

Constants

View Source
const (
	DEFAULT_HASH_LENGTH = 32
	// DEFAULT_CPU = 32768
	DEFAULT_CPU = 8192 // CPU cost, usually is 32768, higher takes more resources
	DEFAULT_R   = 8    // Repetition , higher takes more resources, default = 8
	DEFAULT_P   = 1    // Permutation?, higher takes more resources, default = 1
)
View Source
const (
	MAX_OTP_DIGIT = 10 // max number of otp digit in total (set * digit)
	MIN_OTP_DIGIT = 2  // min number of otp digit per set

	DEFAULT_OTP_DIGIT     = 2 // default otp digit per set
	DEFAULT_OTP_SET       = 2 // default otp set
	DEFAULT_OTP_SEPARATOR = "-"
	DEFAULT_OTP_ERROR     = "7" // use if randomizer is error, then random number is always this const

	DEFAULT_TOKEN_ITERATION = 5 // iterate randomtoken to get long random string. Used in magic link?
)

Variables

This section is empty.

Functions

func CreateHash

func CreateHash(key string) string

CreateHash generates a 128-bit/32-character MD5 hash, useful for AES key generation. Usage: hash := CreateHash("my-secret-key") Output: "5ebe2294ecd0e0f08eab7690d2a6ee69"

func CreateJWE

func CreateJWE(payload []byte, key []byte) (string, error)

Standard JWE format BASE64URL(UTF8(Protected Header)).BASE64URL(Encrypted Key).BASE64URL(IV).BASE64URL(Ciphertext).BASE64URL(Authentication Tag) Protected Header: {"alg":"A128GCM","enc":"A128GCM"} Encrypted Key: Encrypted symmetric key (optional, not used in this example) IV: Initialization Vector (nonce) Ciphertext: Encrypted payload Authentication Tag: Used for integrity check (not used in this example) NOTE: This is a simplified example. In a real-world application, you should handle errors and edge cases properly. Create JWE token The payload is the data you want to encrypt The key is the symmetric key used for encryption

func DecryptWithKey

func DecryptWithKey(data, key string) (string, error)

Decrypt login payload DecryptWithKey decrypts a Base64-encoded ciphertext using AES in CFB mode with the provided key. Usage: decrypted, err := DecryptWithKey(encrypted, "my32byteencryptionkey!") Output: "my secret data"

func EncryptWithKey

func EncryptWithKey(data, key string) (string, error)

func GenerateDefaultOTP

func GenerateDefaultOTP() string

func GenerateOTP

func GenerateOTP(digit, set int, separator string) string

Get random number for OTP (6 digit format with dash) Generate random number in form of string usually used for OTP The digit is number of digit, set is how many sets are there Ex: 57-03 ==> digit=2, set=2 . 820-587 ==> digit=3, set=2

func GenerateSecureRandomNumber

func GenerateSecureRandomNumber(numLen int) (string, error)

GenerateSecureRandomNumber generates a secure random number of the specified number of digits. It uses crypto/rand to ensure the randomness is cryptographically secure. The generated number is a string of digits. If an error occurs during random number generation, it returns an empty string and the error. The function uses a character set of digits (0-9) to generate the random number. The length of the generated number is determined by the numLen parameter. The function is suitable for generating secure random numbers for various purposes, such as OTPs or tokens. Example usage:

numLen := 6
randomNumber, _ := GenerateSecureRandomNumber(numLen)
fmt.Println("Secure random number:", randomNumber)

Output: "102345"

func GetAuthorizationFromHeader

func GetAuthorizationFromHeader(authstring string) (string, string)

Header: Authorization bearer [token] the authstring passed is the "bearer [token]" This is basically splitting the string only. Get authorization from header, return: Bearer 'token'

func GetClientIDSecretFromTokenString

func GetClientIDSecretFromTokenString(jwtKey string) (string, string, error)

From Authorization : Basic [This part is JWTKey] Format accepted is JWTKey == base64(ID:SECRET) Authorization Basic JWTKey

func GetJWTClaimMapFromTokenString

func GetJWTClaimMapFromTokenString(t, JWTKey string) (jwt.MapClaims, error)

Get JWT Claim manually (without using the JWT middleware) Parse the header manually then get the JWT. This function is needed to check if JWT is valid but expired, then we use it to renew/extends the expiration

func HashPin

func HashPin(pin, key1, key2 string) (string, error)

Encrypt Meda password Password is in variable pin key1 and key2 is the salt. Usually key1 = signature and key2 = the additional randomness but that cannot change, usually is the created_at date string HashPin hashes a PIN using scrypt with salts (key1 and key2). Usage: hash, err := HashPin("1234", "signature", "2025-04-16") Output: Base64-encoded hash (e.g., "c29tZS1oYXNoLXZhbHVl")

func MD5Hash

func MD5Hash(text string) string

MD5Hash generates a 32-character MD5 hash of the input string. Usage: hash := MD5Hash("example") Output: "1a79a4d60de6718e8e5b326e338ae533"

func NewRandomToken

func NewRandomToken() string

Generate just random token which is essentially a short-uuid, 22 characters length (this golang implementation) But actually short-uuid can be different length on different implementation or different programming language While standard UUID format is always 36 characters long. Also this is base57 encoding which exclude characters that is simmilar like 0 and O and l and I

func NewRandomTokenIterate

func NewRandomTokenIterate(x int) string

Concatenante NewRandomToken (which is short-uuid) x number of times. This is to be used in maybe magic link or public link which takes longer string.

func PGPDecrypt

func PGPDecrypt(privKey, encryptedMessage string) (string, error)

PGPDecrypt decrypts an ASCII-armored encrypted message using the recipient's PGP private key. Usage: decrypted, err := PGPDecrypt(privKey, encryptedMessage) Output: "Hello, World!"

func PGPEncrypt

func PGPEncrypt(pubKey, message string) (string, error)

PGPEncrypt encrypts a message using the recipient's PGP public key. Usage: encrypted, err := PGPEncrypt(pubKey, "Hello, World!") Output: ASCII-armored encrypted message

func PGPGenerateKey

func PGPGenerateKey(name, comment, email string) (string, string, error)

PGPGenerateKey generates a PGP key pair (public and private keys). Usage: pubKey, privKey, err := PGPGenerateKey("John Doe", "Comment", "john@example.com") Output: Public and private keys as strings

func ParseJWE

func ParseJWE(jweString string, key []byte) ([]byte, error)

Parse JWE token The JWE string is the token you want to decrypt The key is the symmetric key used for decryption The function returns the decrypted payload Example usage: jweString := "eyJhbGciOiJBMjU2R0NNIiwiZW5jIjoiQTEyOEdDTSJ9.h79q

func ParseJWEToMap

func ParseJWEToMap(jweString string, key []byte) (map[string]string, error)

If the cypertext or basically the payload is json of map[string]string then this has the unmarshall and return the map[string]string NOTE: later if needed use MapToStruct from utils

func SHA256

func SHA256(s string) string

SHA256 generates a 64-character SHA-256 hash of the input string. Usage: hash := SHA256("example") Output: "50d858e8e8c1b6f1c8b8e8e8e8e8e8e8e8e8e8e8e8e8e8e8e8e8e8e8e8e8e8e8"

Types

This section is empty.

Jump to

Keyboard shortcuts

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