crypto

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2024 License: MIT Imports: 17 Imported by: 0

README

Crypto

Crypto package implements state-of-the-art hashing algorithms and elliptic helper functions.

Usage
Random string
package main

import "github.com/infiniteloopcloud/crypto"

func main() {
	// Generates a cryptographically secure random string
	crypto.RandomString(10)
}
Hashing
package main

import "github.com/infiniteloopcloud/crypto"

func main() {
	// Get a crypto algorithm, options: Argon2id
	alg, _ := crypto.Get(crypto.Argon2id)
	
	// Generate salt and hash a string
	salt := crypto.RandomString(10)
	hash := alg.Hash("data", salt)
	
	// Check data against the hash
	alg.Verify("data", salt, hash)
}
Token generation
package main

import (
	"fmt"
	"github.com/infiniteloopcloud/crypto"
)

func main() {
	// Get a crypto algorithm, options: Argon2id
	alg, _ := crypto.Get(crypto.Argon2id)

	// Generate cryptographically secure token
	token := alg.GenerateToken("salt")
	fmt.Println(token)
}
Encrypt/Decrypt

This functionality makes it easier to encrypt arbitrary length strings with AES-128

package main

import (
	"log"

	"github.com/infiniteloopcloud/crypto"
)

func main() {
	dataToEncrypt := "testdatatestdatatestdatatestdatatestdatatestdata1234"

	// NOTE: The key and the IV always should be 16 length
	e := crypto.NewEncypter("bhXRirFB8IaQxxjm", "oZDRWCHryRtlVA1I")
	resultData, err := e.Encrypt(dataToEncrypt)
	if err != nil {
		// handle error
	}

	decrypted, err := e.Decrypt(resultData)
	if err != nil {
		// handle error
	}
	if decrypted != dataToEncrypt {
		log.Printf("Decrypted is not the original data, original: %s, decrypted: %s", dataToEncrypt, decrypted)
	}
}
Elliptic curve helpers
  • MarshalECPublicKey accept an ecdsa.PublicKey and marshal it to a compressed shareable format
  • UnmarshalECPublicKey accept a compressed format and parse to an ecdsa.PublicKey

Documentation

Overview

Package crypto provides cryptographic solutions for the user package. The main responsibility to define one or more hashing methods to store passwords and other sensitive data securely.

Index

Constants

View Source
const (
	// Argon2id defines a type for argon2 hashing as enum
	Argon2id uint8 = iota
)
View Source
const (
	// DefaultSaltLength defines the default length when salt generation happening
	DefaultSaltLength = 18
)

Variables

View Source
var (
	ErrAES128Length          = errors.New("plaintext length should be 16")
	ErrInternalDecryptLength = errors.New("decrypt data not formatted properly")
	ErrKeyLength             = errors.New("key should be at least 1 character")
)

errors

Functions

func GenerateCurve added in v0.4.0

func GenerateCurve() (string, error)

func MarshalECPublicKey

func MarshalECPublicKey(publicKey ecdsa.PublicKey) string

MarshalECPublicKey accept an ecdsa.PublicKey and marshal it to a compressed shareable format

func Padding

func Padding(key string, length int) (string, error)

func RandomString

func RandomString(n int) string

RandomString generates random bytes and returns as string

func UnmarshalECPublicKey

func UnmarshalECPublicKey(compressed string) (ecdsa.PublicKey, error)

UnmarshalECPublicKey accept a compressed format and parse to an ecdsa.PublicKey

Types

type AES128

type AES128 struct{}

func (AES128) Decrypt

func (a AES128) Decrypt(ciphertext, key string, iv []byte) (string, error)

Decrypt the main decryption endpoint where the ciphertext decrypted

func (AES128) Encrypt

func (a AES128) Encrypt(plaintext, key string, iv []byte) (string, error)

Encrypt the main encryption endpoint where the plaintext encrypted with the arbitrary length key and the constant initialization vector

func (AES128) HashSize

func (a AES128) HashSize() int

HashSize ...

type Descriptor

type Descriptor interface {
	// Hash should be able to do a hashing on an arbitrary string
	Hash(str, salt string) string

	// Verify should be able to verify a previously created hash
	// by compering with the arbitrary string
	Verify(str, salt, hash string) error

	// GenerateToken should be able to generate a token
	// which hardened with the hashing algorithm
	GenerateToken(tokenSalt string) string
}

Descriptor defines what a hashing implementation should be able to do

func Get

func Get(typ uint8) (Descriptor, error)

Get will return a hash algorithm for use, or returns error

type Encrypter

type Encrypter interface {
	Encrypt(data string) (string, error)
	Decrypt(data string) (string, error)
}

func NewEncypter

func NewEncypter(key, iv string) Encrypter

Jump to

Keyboard shortcuts

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