confcrypt

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2024 License: MIT Imports: 12 Imported by: 2 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

confcrypt GoDoc

Encrypt your sensitive data in config file and decrypt it at runtime.

Get

go get -u "github.com/WqyJh/confcrypt"

Usage

Use any random string as your config key.

Use EncryptString to encrypt all of your sensitive data one by one.

var (
	key = "12345678"
)

func TestEncrypt(t *testing.T) {
	plain := "hello"
	encrypted, err := confcrypt.EncryptString(plain, key)
	assert.NoError(t, err)
	t.Logf("encrypted: '%s'", encrypted)

	decrypted, err := confcrypt.Decrypt(encrypted[4:], key)
	assert.NoError(t, err)
	t.Logf("decrypted: '%s'", decrypted)
}

func TestDecrypt(t *testing.T) {
	encrypted := "ENC~tSbCaeksELsWsw9+eXADFTRONqOTiPkL6q5yRW8Wp4Um"
	decrypted, err := confcrypt.Decrypt(encrypted[4:], key)
	assert.NoError(t, err)
	t.Logf("decrypted: '%s'", decrypted)
}

Replace all of your sensitive data in your config with the encrypted string, which should start with ENC~.

And use Decrypt to decrypt the encrypted string without ENC~ prefix.

It's recommended to use Decode to decrypt all encrypted string fields in a struct.

For example, assume you have config struct as below:

type AppConfig {
    Id string
    Secret string
}
type Config struct {
    User string
    Password string
    App AppConfig
}

You load it from config file and got:

cfg := Config{
    User: "foo",
    Password: "ENC~xxxxxxxxxxxxxxxxxxxxx",
    App: AppConfig{
        Id: "bar",
        Secret: "ENC~yyyyyyyyyyyyyyyyyyyy",
    },
}

Then use Decode to decrypt the encrypted fields including Password and Secret.

result, err := confcrypt.Decode(&cfg, key)
// result.Password is decrypted
// result.App.Secret is decrypted

License

Released under the MIT License.

Documentation

Overview

Package confcrypt provides a simple interface for encrypting and decrypting your config.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidCipherText = errors.New("invalid text to decrypt")
)

Functions

func Decode

func Decode(obj interface{}, key string) (interface{}, error)

Decode decrypts the encrypted string fields start with `ENC~` in fields tree of obj and returns the decrypted obj.

func DecodeByEnv added in v0.2.0

func DecodeByEnv(v interface{}, opts ...DecodeOption) error

func DecodeInplace added in v0.2.0

func DecodeInplace(v interface{}, key string) error

func Decrypt

func Decrypt(text string, passphrase string) ([]byte, error)

Decrypt decrypts text with passphrase using AES-256 algorithm.

func Encrypt

func Encrypt(data []byte, passphrase string) (string, error)

Encrypt encrypts data with passphrase using AES-256 algorithm.

func EncryptString

func EncryptString(s string, key string) (string, error)

EncryptString encrypts string with passphrase using AES-256 algorithm and prepends "ENC~" to the result.

Types

type DecodeOption added in v0.2.0

type DecodeOption func(*decodeOption)

func WithEnv added in v0.2.0

func WithEnv(env string) DecodeOption

Jump to

Keyboard shortcuts

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