Documentation ¶
Overview ¶
Package gocrypt provide a library for do encryption in struct with go field.
Package gocrypt provide in struct tag encryption or inline encryption and decryption ¶
The package supported:
DES3 — Triple Data Encryption Standard ¶
AES/GCM — Advanced Encryption Standard
RC4 — stream chipper ¶
The AES cipher is the current U.S. government standard for all software, and is recognized worldwide.
The DES ciphers are primarily supported for PBE standard that provides the option of generating an encryption key based on a passphrase.
The RC4 is supplied for situations that call for fast encryption, but not strong encryption. RC4 is ideal for situations that require a minimum of encryption.
Index ¶
- Constants
- func DecryptWithPrivateKey(ciphertext []byte, priv *rsa.PrivateKey) ([]byte, error)
- func EncryptWithPublicKey(msg []byte, pub *rsa.PublicKey) ([]byte, error)
- func ExportPrivateKey(key *rsa.PrivateKey) []byte
- func ExportPublicKey(pub *rsa.PublicKey) []byte
- func GenerateKeyPair(bits int) (*rsa.PrivateKey, *rsa.PublicKey)
- func ParsePrivateKey(key []byte) (*rsa.PrivateKey, error)
- func ParsePublicKey(key []byte) (*rsa.PublicKey, error)
- type AESOpt
- type DESOpt
- type GocryptInterface
- type GocryptOption
- type Option
- type RC4Opt
Examples ¶
Constants ¶
const (
// GOCRYPT is variable tag field for gocrypt
GOCRYPT = "gocrypt"
)
Variables ¶
This section is empty.
Functions ¶
func DecryptWithPrivateKey ¶
func DecryptWithPrivateKey(ciphertext []byte, priv *rsa.PrivateKey) ([]byte, error)
DecryptWithPrivateKey decrypts data with private key.
func EncryptWithPublicKey ¶
EncryptWithPublicKey encrypts data with public key.
func ExportPrivateKey ¶
func ExportPrivateKey(key *rsa.PrivateKey) []byte
ExportPrivateKey exports private key to bytes.
func ExportPublicKey ¶
ExportPublicKey exports public key to bytes.
func GenerateKeyPair ¶
func GenerateKeyPair(bits int) (*rsa.PrivateKey, *rsa.PublicKey)
GenerateKeyPair generates a new key pair.
func ParsePrivateKey ¶
func ParsePrivateKey(key []byte) (*rsa.PrivateKey, error)
ParsePrivateKey parses private key bytes to *rsa.PrivateKey.
Types ¶
type AESOpt ¶
type AESOpt struct {
// contains filtered or unexported fields
}
AESOpt contains all aes session option
func NewAESOpt ¶
NewAESOpt is function to create new configuration of aes algorithm option the secret must be hexa a-f & 0-9
Example ¶
package main import ( "fmt" "log" "github.com/bingoohuang/gg/pkg/cry" ) func main() { // sample plain text sampleText := "Halo this is encrypted text!!!" // it's random string must be hexa a-f & 0-9 const key = "fa89277fb1e1c344709190deeac4465c2b28396423c8534a90c86322d0ec9dcf" // define AES option aesOpt, err := cry.NewAESOpt(key) if err != nil { log.Println("ERR", err) return } // Encrypt text using AES algorithm cipherText, err := aesOpt.Encrypt([]byte(sampleText)) if err != nil { log.Println("ERR", err) return } // 每次加密结果不一样(因为加入了随机的nonce) // fmt.Println("Encrypted data", string(cipherText)) // Encrypted data 28509ae15364b3994846eeb4079122825285ac7863f94fab43aacb43b5a81e2f93c58743c5d089ae4fefe468b1e240ac88cb50e9316a2f618d55 // Encrypted data b91a0019778033730704da5881a39c955f58c878f6a7938da107a2cbb9d752100af56024463d901c75b1ea6cb63089bf88e8b758b9786de9150e // Decrypt text using AES algorithm plainText, err := aesOpt.Decrypt([]byte(cipherText)) if err != nil { log.Println("ERR", err) return } fmt.Println("Decrypted data", string(plainText)) }
Output: Decrypted data Halo this is encrypted text!!!
type DESOpt ¶
type DESOpt struct {
// contains filtered or unexported fields
}
DESOpt contains all DES session option
func NewDESOpt ¶
NewDESOpt is function to create new configuration of des algorithm option the secret must be 24 character
Example ¶
package main import ( "fmt" "log" "github.com/bingoohuang/gg/pkg/cry" ) func main() { // sample plain text sampleText := "Halo this is encrypted text!!!" // it's character 24 bit const key = "123456781234567812345678" // define DES option desOpt, err := cry.NewDESOpt(key) if err != nil { log.Println("ERR", err) return } // Encrypt text using DES algorithm cipherText, err := desOpt.Encrypt([]byte(sampleText)) if err != nil { log.Println("ERR", err) return } // fmt.Println("Encrypted data:", cipherText) // 每次都得到一样的数据 // Encrypted data: k1Uoi4OsCMBSCeVxdBmwfVuO2PxndJZSfCsXIULB7F0= // Decrypt text using DES algorithm plainText, err := desOpt.Decrypt([]byte(cipherText)) if err != nil { log.Println("ERR", err) return } fmt.Println("Decrypted data:", plainText) }
Output: Decrypted data: Halo this is encrypted text!!!
type GocryptInterface ¶
GocryptInterface is facing the format gocrypt option library
type GocryptOption ¶
type GocryptOption interface { Encrypt(plainText []byte) (string, error) Decrypt(chipherText []byte) (string, error) }
GocryptOption is facing the format encryption and decryption format
type Option ¶
type Option struct { AESOpt GocryptOption DESOpt GocryptOption RC4Opt GocryptOption Custom map[string]GocryptOption Prefix string Postfix string }
Option contains an option from initial algorithm encryptioin & decryption.
func New ¶
New create and initialize new option for struct field encryption.
It needs option from aes, rc4, or des for initialitaion
type RC4Opt ¶
type RC4Opt struct {
// contains filtered or unexported fields
}
RC4Opt is tructure of aes option
func NewRC4Opt ¶
NewRC4Opt is function to create new configuration of aes algorithm option the secret must be hexa a-f & 0-9
Example ¶
package main import ( "fmt" "log" "github.com/bingoohuang/gg/pkg/cry" ) func main() { // sample plain text sampleText := "Halo this is encrypted text!!!" // it's character 24 bit const key = "123456781234567812345678" // define RC4 option rc4Opt, err := cry.NewRC4Opt(key) if err != nil { log.Println("ERR", err) return } // Encrypt text using RC4 algorithm cipherText, err := rc4Opt.Encrypt([]byte(sampleText)) if err != nil { log.Println("ERR", err) return } // fmt.Println("Encrypted data:", cipherText) // 每次都得到一样的数据 // Encrypted data: f39255bb29c5b6ce831363bc865866c600e7ed3dac0c5dc13c63a196788c // Decrypt text using RC4 algorithm plainText, err := rc4Opt.Decrypt([]byte(cipherText)) if err != nil { log.Println("ERR", err) return } fmt.Println("Decrypted data:", plainText) }
Output: Decrypted data: Halo this is encrypted text!!!