secret

package
v0.0.0-...-9823ec8 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2021 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Example
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

c, err := New(ctx, "base64key://smGbjm71Nxd1Ig5FS0wj9SlbzAIrnolCz9bQQ6uAhl4=")
if err != nil {
	fmt.Println(err)
	return
}
defer c.Close()

plainKey := make([]byte, 32)
_, _ = rand.Read(plainKey)

cypherKey, err := c.EncryptKey(ctx, plainKey)
if err != nil {
	fmt.Println(err)
	return
}

plainText := "asdfghjklñqwertyuiozxcvbnm,"

cypherText, err := c.Encrypt(ctx, []byte(plainText), cypherKey)
if err != nil {
	fmt.Println(err)
	return
}

result, err := c.Decrypt(ctx, cypherText, cypherKey)
if err != nil {
	fmt.Println(err)
	return
}

if r := string(result); r != plainText {
	fmt.Printf("unexpected result: %s", r)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var OpenCensusViews = secrets.OpenCensusViews

Functions

func Decrypt

func Decrypt(data []byte, passphrase []byte) ([]byte, error)
Example
msg := "zxcvbnmasdfghjklqwertyuiop1234567890"
passphrase := "some secret"

cypherMsg1, err := Encrypt([]byte(msg), []byte(passphrase))
if err != nil {
	fmt.Println(err)
	return
}

cypherMsg2, err2 := Encrypt([]byte(msg), []byte(passphrase))
if err2 != nil {
	fmt.Println(err2)
	return
}

if string(cypherMsg1) == string(cypherMsg2) {
	fmt.Println("two executions with the same input shall not generate the same output")
	return
}

res1, err3 := Decrypt(cypherMsg1, []byte(passphrase))
if err != nil {
	fmt.Println(err3)
	return
}

res2, err4 := Decrypt(cypherMsg2, []byte(passphrase))
if err != nil {
	fmt.Println(err4)
	return
}

if string(res1) != string(res2) {
	fmt.Println("results are different:", string(res1), string(res2))
	return
}
Output:

func Encrypt

func Encrypt(data []byte, passphrase []byte) ([]byte, error)
Example
msg := "zxcvbnmasdfghjklqwertyuiop1234567890"
passphrase := "some secret"

cypherMsg, err := Encrypt([]byte(msg), []byte(passphrase))
if err != nil {
	fmt.Println(err)
	return
}

cypherMsg2, err2 := Encrypt([]byte(msg), []byte(passphrase))
if err2 != nil {
	fmt.Println(err2)
	return
}

if string(cypherMsg) == string(cypherMsg2) {
	fmt.Println("two executions with the same input shall not generate the same output")
}
Output:

Types

type Cypher

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

func New

func New(ctx context.Context, url string) (*Cypher, error)

func (*Cypher) Close

func (c *Cypher) Close()

func (*Cypher) Decrypt

func (c *Cypher) Decrypt(ctx context.Context, cipherText, cipheredKey []byte) ([]byte, error)

func (*Cypher) Encrypt

func (c *Cypher) Encrypt(ctx context.Context, plainText, cipheredKey []byte) ([]byte, error)

func (*Cypher) EncryptKey

func (c *Cypher) EncryptKey(ctx context.Context, plainKey []byte) ([]byte, error)

Jump to

Keyboard shortcuts

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