https://github.com/clavoie/cryptu
Injectable base64 symmetric encryption wrappers for Go
https://github.com/clavoie/cryptu
base64-encoding go golang symmetric-encryption
Last synced: about 2 months ago
JSON representation
Injectable base64 symmetric encryption wrappers for Go
- Host: GitHub
- URL: https://github.com/clavoie/cryptu
- Owner: clavoie
- License: mit
- Created: 2018-08-30T12:03:47.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-09-03T19:47:30.000Z (over 6 years ago)
- Last Synced: 2025-04-12T00:49:43.116Z (about 2 months ago)
- Topics: base64-encoding, go, golang, symmetric-encryption
- Language: Go
- Size: 21.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# cryptu [](http://godoc.org/github.com/clavoie/cryptu) [](https://travis-ci.org/clavoie/cryptu) [](https://codecov.io/gh/clavoie/cryptu) [](https://goreportcard.com/report/github.com/clavoie/cryptu)
Base64 symmetric encryption wrappers for Go, with hooks to be used by dependency injection.
## Encrypt To Base64
The package level functions `EncryptToBase64` and `DecryptFromBase64` use the `crypto/aes` package for encryption and `encoding/base64.StdEncoding` for the encoding.
```go
// keys must be 16, 24, or 32 in length
key := "zqpf8VWyrUP9j1gC"
secret := "sensitive data"encryptedValue, err := cryptu.EncryptToBase64(key, secret)
if err != nil {
log.Fatal(err)
}fmt.Println(encryptedValue)
// R+gUlOWekeVALBOntneoP7wQK2IOBiC3ddS+Rj2xdecodedValue, err := base64.StdEncoding.DecodeString(encryptedValue)
if err != nil {
log.Fatal(err)
}fmt.Println(string(decodedValue))
// G¦¦?¦@,¦¦w¦?¦+b ¦u?F=¦decryptedSecret, err := cryptu.DecryptFromBase64(key, encryptedValue)
if err != nil {
log.Fatal(err)
}fmt.Println(decryptedSecret)
// sensitive data
```
A full example is available [here](https://godoc.org/github.com/clavoie/cryptu#example-EncryptToBase64)## Dependency Injection
An interface is provided to wrap all top level package functions. This interface can be injected the into your code instead of calling the package functions directly. There are some [predefined dependency definitions](https://github.com/clavoie/cryptu/blob/master/di.go#L7) provided by the package. If you'd like to start using them you need only supply a definition for `cryptu.Key` and `cryptu.Base64Encoding`:
```go
func NewKey() (cryptu.Key, error) { return cryptu.NewStrKey(os.Getenv("MY_KEY")) }
func NewEncoding() (cryptu.Base64Encoding, error) { return cryptu.NewBase64Encoding(base64.StdEncoding) }var defs = []*di.Def{
{NewKey, di.Singleton},
{NewEncoding, di.Singleton},
}func MyHandler(encoder cryptu.Base64) {
encryptedValue, err := encoder.Encrypt("some value")
// if err
myResponse := &MyResponse{Secret: encryptedValue}
// write response
}func main() {
resolver, err := di.NewResolver(errFn, cryptu.NewDiDefs(), defs)
// if err
handler, err := resolver.HttpHandler(MyHandler)
// if err
http.HandleFunc("/foo", handler)
// listen and serve
}
```A full example is available [here](https://godoc.org/github.com/clavoie/cryptu#example-Base64)