symcrypto

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2017 License: MIT Imports: 4 Imported by: 0

README

symcrypto

Build Status Coverage Status GoDoc Go Report Card

A URL safe, string encryption/decryption library.

Encryption and decryption is done using a secret key. secretbox is used under the hood.

Install

# using dep(recommended)
$ dep ensure -add github.com/sudo-suhas/symcrypto

# using go get
$ go get -u github.com/sudo-suhas/symcrypto

Usage

package main

import (
	"fmt"

	"github.com/sudo-suhas/symcrypto"
)

func handleError(err error) {
	if err != nil {
		panic(err)
	}
}

func main() {
	// Load your secret key from a safe place and use it to create an instance of
	// `Crypter`. (Obviously don't use this example key for anything real.) If you want
	// to convert a passphrase to a key, use a suitable package like bcrypt or scrypt.
	crypto, err := symcrypto.New("6368616e676520746869732070617373")
	handleError(err)

	// This returns the nonce appended with the encrypted string for "hello world".
	encrypted, err := crypto.Encrypt("hello world")
	handleError(err)

	fmt.Printf("Encrypted message - %q\n", encrypted)
	// Output: Encrypted message - "2eUERfII6K7djr-GCR2qwSf8LJ-7ZoOmdlT54HPkhw297ML46M6VvlpvW2LrA_Ewge-2"
	// Because of unique nonce, encrypted message will vary for the same input.

	// The encrypted string can be decrypted only by using the `Crypter` instance which
	// was used to encrypt it.
	decrypted, err := crypto.Decrypt(encrypted)
	handleError(err)

	fmt.Printf("Decrypted message - %q\n", decrypted)
	// Output: Decrypted message - "hello world"
}

License

MIT © Suhas Karanth

Documentation

Overview

Package symcrypto is a URL safe encryption/decryption library.

It uses golang.org/x/crypto/nacl/secretbox under the hood which is suitable for encrypting small messages. "encoding/base64" is used to make the encrypted token URL safe.

Example
package main

import (
	"fmt"

	"github.com/sudo-suhas/symcrypto"
)

func handleError(err error) {
	if err != nil {
		panic(err)
	}
}

func main() {
	// Load your secret key from a safe place and use it to create an instance of
	// `Crypter`. (Obviously don't use this example key for anything real.) If you want
	// to convert a passphrase to a key, use a suitable package like bcrypt or scrypt.
	crypto, err := symcrypto.New("6368616e676520746869732070617373")
	handleError(err)

	// This returns the nonce appended with the encrypted string for "hello world".
	encrypted, err := crypto.Encrypt("hello world")
	handleError(err)

	// Example encrypted message - "2eUERfII6K7djr-GCR2qwSf8LJ-7ZoOmdlT54HPkhw297ML46M6VvlpvW2LrA_Ewge-2"
	// Because of unique nonce, encrypted message will vary for the same input.

	// The encrypted string can be decrypted only by using the `Crypter` instance which
	// was used to encrypt it.
	decrypted, err := crypto.Decrypt(encrypted)
	handleError(err)

	fmt.Printf("Decrypted message - %q\n", decrypted)
}
Output:

Decrypted message - "hello world"

Index

Examples

Constants

View Source
const SecretKeyLen = 32

SecretKeyLen is the minimum length of secret key required for creating an instance of Crypter. NaCl expects the secret key to be 32 characters long.

Variables

This section is empty.

Functions

This section is empty.

Types

type Crypter

type Crypter interface {
	// Encrypt encrypts the given message using the configured secret key and returns the
	// encrypted string. The encrypted string is encoded using base64 so that it can be
	// used in the URL.
	Encrypt(string) (string, error)

	// Decrypt decrypts and returns the given token using the configured secret key. The
	// encrypted string is expected to be base64 encoded.
	Decrypt(string) (string, error)
}

Crypter does encryption and decription using the given secret key. The encrypted string is URL safe via base64 encoding.

func New

func New(secret string) (Crypter, error)

New creates an instance of Crypter which can be used for encrypting/decrypting with the same secret key.

It is recommedded to load your secret key from a safe place and use it for instantiating a Crypter instance. If you want to convert a passphrase to a key, use a suitable package like bcrypt or scrypt. The secret key must be at least 32 chars long. If the length exceeds 32 chars, the mid 32 chars will be used as the secret key.

Jump to

Keyboard shortcuts

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