crypt

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2025 License: MIT Imports: 15 Imported by: 0

README

package crypt

Function Comment
func NewBcrypt() Bcrypt creates an bcrypt handler
func (b *bcryptProperties) Encrypt(plainText string) (string, error) encrypt plaintext
func (b *bcryptProperties) HashedText(hashedText string) set a hashed text string
func (b *bcryptProperties) PlainText(plainText string) set a plain text string
func (b *bcryptProperties) Cost(cost int) sets bcrypt costs
func (b *bcryptProperties) Compare() bool checks the hash against the plaintext
func GenerateEd25519KeyFiles(dir string, filename string) (string, error) generates ed25519 key files (id_ed25519 and id_ed25519.pub)
func NewEncryptedString(plainTextValue string) EncryptedString creates an Encrypted string
func NewDecryptedString(encryptedValue string) string decrypt an encrypted string
func (v EncryptedString) Value() string returns the decrypted plainTextValue
func (v EncryptedString) String() string returns the decrypted plainTextValue
func (v EncryptedString) MarshalText() ([]byte, error)
func (v *EncryptedString) UnmarshalText(text []byte) error
func (v EncryptedString) MarshalBinary() ([]byte, error)
func NewSymmetricEncryption() *SymCrypt creates an SymCrypt handler
func (s *SymCrypt) SetKey(key string) *SymCrypt set an AES key
func (s *SymCrypt) SetPlainText(plainText string) *SymCrypt set plain text to encrypt.
func (s *SymCrypt) GetCypherBase64() string returns the encrypted data stream as base64 encoded
func (s *SymCrypt) SetCypherBase64(base64String string) *SymCrypt set cipher text as base64 string.
func (s *SymCrypt) GetPlainText() (string, error) returns the plaintext

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateEd25519KeyFiles

func GenerateEd25519KeyFiles(dir string, filename string) (string, error)

GenerateEd25519KeyFiles generates two files id_ed25519 and id_ed25519.pub in directory dir. Source: https://github.com/mikesmitty/edkey/blob/master/edkey.go. It returns the fully qualified path to the public key file.

func NewDecryptedString

func NewDecryptedString(encryptedValue string) string

NewDecryptedString decrypts an encrypted string

Types

type Bcrypt

type Bcrypt interface {
	// Encrypt takes plainText and encrypts it with bcrypt and its costs (default = 4).
	// It returns the encrypted string or an empty string on error.
	Encrypt(plainText string) (string, error)

	// Cost sets bcrypt costs (and checks its bounds)
	Cost(cost int)

	// HashedText allows to set a hased text string like e.g. $2a$04$vH2ABneGM7Lpl0oGkvAgtOCIF29Ku.TUPx5.CLD1.EKLl2RgT6ilK
	HashedText(hashedText string)

	// PlainText allows to set a plain text string
	PlainText(plainText string)

	// Compare checks the hash against the plaintext (hashed) and returns true
	// on success
	Compare() bool
}

Bcrypt is the main interface

func NewBcrypt

func NewBcrypt() Bcrypt

NewBcrypt is the main entry point

type EncryptedString

type EncryptedString struct {
	// contains filtered or unexported fields
}

EncryptedString is a sym crypt string. It automatically gets encrypted when created. It also implements the Marshall/Unmarshall Text/Binary go interfaces for easier usage.

func NewEncryptedString

func NewEncryptedString(plainTextValue string) EncryptedString

NewEncryptedString creates a new EncryptedString by encrypting the supplied plainTextValue.

func (EncryptedString) MarshalBinary

func (v EncryptedString) MarshalBinary() ([]byte, error)

func (EncryptedString) MarshalText

func (v EncryptedString) MarshalText() ([]byte, error)

func (EncryptedString) String

func (v EncryptedString) String() string

Value returns the decrypted plainTextValue.

func (*EncryptedString) UnmarshalBinary

func (v *EncryptedString) UnmarshalBinary(text []byte) error

func (*EncryptedString) UnmarshalText

func (v *EncryptedString) UnmarshalText(text []byte) error

func (EncryptedString) Value

func (v EncryptedString) Value() string

Value returns the decrypted plainTextValue.

type SymCrypt

type SymCrypt struct {
	// contains filtered or unexported fields
}

SymCrypt is the main structure for symmetric encryption and holds plainText, key and cipher as unexported fields.

Usage sample:

// cipher e.g. "gQTf9p9trkvx3xvSKAQhLIfqUWirxnf5Sl+YzAKV5zjS0hLZ0LRXs+1rb6M="
cipher := crypt.NewSymmetricEncryption().SetPlainText("mySecretPassword").GetCypherBase64()
plain := crypt.NewSymmetricEncryption().SetCypherBase64(cipher).GetPlainText()
if plain != "mySecretPassword" {
	log.Println("ERROR: expected 'mySecretPassword'")
}

func NewSymmetricEncryption

func NewSymmetricEncryption() *SymCrypt

NewSymmetricEncryption is the entry point for AES encryption/decryption and can be used to encrypt a string and get the result as base64 string. When the key is not configured it uses a built in one (should not be used in production environment).

func (*SymCrypt) GetCypherBase64

func (s *SymCrypt) GetCypherBase64() string

GetCypherBase64 returns the encrypted data stream as base64 encoded string like e.g. 8q+orlJS5rzn0HtzbmFIkJIGAoOIL3zczlXVTUylRU021g==. It returns an empty string on error

func (*SymCrypt) GetPlainText

func (s *SymCrypt) GetPlainText() (string, error)

GetPlainText returns the plaintext

func (*SymCrypt) SetCypherBase64

func (s *SymCrypt) SetCypherBase64(base64String string) *SymCrypt

SetCypherBase64 adds s.cipher - but as base64 string.

func (*SymCrypt) SetKey

func (s *SymCrypt) SetKey(key string) *SymCrypt

SetKey adds an AES key - the length is not so important because it is stripped or expanded to exactly 32 chars (256 bit).

func (*SymCrypt) SetPlainText

func (s *SymCrypt) SetPlainText(plainText string) *SymCrypt

SetPlainText text adds the text we want to encrypt.

Jump to

Keyboard shortcuts

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