crypto

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2017 License: MIT Imports: 13 Imported by: 3

README

gowww crypto GoDoc Build Coverage Go Report Status Stable

Package crypto provides encryption and hashing utilities.

Installing

  1. Get package:

    go get -u github.com/gowww/crypto
    
  2. Import it in your code:

    import "github.com/gowww/crypto"
    

Usage

Use NewEncrypter with a 32 bytes long secret key to make a new Encrypter:

encrypter, _ := crypto.NewEncrypter([]byte("secret-key-secret-key-secret-key"))

Use Encrypter.Encrypt or Encrypter.EncryptBase64 to encrypt a value:

encryptedData, _ := encrypter.Encrypt([]byte("data to encrypt"))

Use Encrypter.Decrypt or Encrypter.DecryptBase64 to decrypt a value:

decryptedData, _ := encrypter.Decrypt(encryptedData)

Documentation

Overview

Package crypto provides encryption and hashing utilities.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	SourceNum           = []byte("0123456789")
	SourceAlphaLower    = []byte("abcdefghijklmnopqrstuvwxyz")
	SourceAlphaUpper    = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
	SourceAlphaNumLower = []byte("abcdefghijklmnopqrstuvwxyz0123456789")
	SourceAlphaNumUpper = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
	SourceAlpha         = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
	SourceAlphaNum      = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
	SourceAll           = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_+=~`[]{}|\\:;\"'<>,.?/")
)

Random sources.

Functions

func HashFileMD5

func HashFileMD5(path string) (string, error)

HashFileMD5 returns the MD5 checksum of a file.

func HashMD5

func HashMD5(src io.Reader) (string, error)

HashMD5 returns the MD5 checksum of source.

func Random

func Random(n int) ([]byte, error)

Random returns random bytes.

func RandomKey

func RandomKey() string

RandomKey returns a random 32 bytes alphanumeric key.

func RandomSourced

func RandomSourced(src []byte, n int) ([]byte, error)

RandomSourced returns a random string only containing bytes from a source string.

Types

type Encrypter

type Encrypter interface {
	Encrypt([]byte) ([]byte, error)
	EncryptBase64([]byte) ([]byte, error)
	Decrypt([]byte) ([]byte, error)
	DecryptBase64([]byte) ([]byte, error)
	HashHS256([]byte) ([]byte, error)
}

An Encrypter provides encryption and hashing methods.

func NewEncrypter

func NewEncrypter(key []byte) (Encrypter, error)

NewEncrypter returns a new AES-128 encrypter for secret key. The key must be 32 bytes long.

Example
package main

import (
	"bytes"
	"fmt"

	"github.com/gowww/crypto"
)

func main() {
	data := []byte("data to encrypt")
	encrypter, _ := crypto.NewEncrypter([]byte("secret-key-secret-key-secret-key"))
	encData, _ := encrypter.Encrypt(data)
	data, _ = encrypter.Decrypt(encData)
	fmt.Println(bytes.Equal(data, encData))
}
Output:

Jump to

Keyboard shortcuts

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