crypto

package module
v3.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2018 License: MIT Imports: 5 Imported by: 0

README

Crypto-go

A functional crypto wrapper for go applications.

Build Status Coverage Status License GoDoc

Documentation

https://godoc.org/github.com/teambition/crypto-go

API

crypto "github.com/teambition/crypto-go":

func Equal(a, b []byte) bool

func RandN(size int) []byte

func HashSum(h func() hash.Hash, data []byte) []byte
func HmacSum(h func() hash.Hash, key, data []byte) []byte

func SHA256Sum(data []byte) []byte
func SHA256Hmac(key, data []byte) []byte

type Rotating []interface{}
func (r Rotating) Verify(fn func(interface{}) bool) (index int)

type RotatingStr []string
func (r RotatingStr) Verify(fn func(string) bool) (index int)

type RotatingBytes [][]byte
func (r RotatingBytes) Verify(fn func([]byte) bool) (index int)

state "github.com/teambition/crypto-go/state":

func Sign(key []byte, message string) string
func Verify(key []byte, message, state string, expire ...time.Duration) bool

func New(keys ...[]byte) (*States, error)
func (s *States) Sign(message string) string
func (s *States) Verify(message, state string, expire ...time.Duration) bool

signature "github.com/teambition/crypto-go/signature":

func Sign(secretKey, message []byte) (sig []byte)
func Verify(secretKey, message, sig []byte) bool

func SignPrivate(privateKey, message []byte) (sig []byte)
func VerifyPublic(publicKey, message, sig []byte) bool

func GenerateKey() (publicKey, privateKey string)
func KeyPairFrom(publicKey string, privateKey ...string) (*KeyPair, error)

func (k *KeyPair) Sign(message []byte) (sig []byte)
func (k *KeyPair) Verify(message, sig []byte) bool

password "github.com/teambition/crypto-go/password":

func Sign(salt []byte, id, pass string, args ...int) (checkPass string)
func Verify(salt []byte, id, pass, checkPass string, args ...int) bool

func New(salt []byte, args ...int) *Password
func (p *Password) Sign(id, pass string) (checkPass string)
func (p *Password) Verify(id, pass, checkPass string) bool

cipher "github.com/teambition/crypto-go/cipher":

func GenerateKey() (publicKey, privateKey string)

func NewBox(publicKey, privateKey string) (*Box, error)
func (b *Box) Encrypt(data []byte) ([]byte, error)
func (b *Box) Decrypt(encrypted []byte) ([]byte, error)

func NewSalsa20(key []byte) (*Salsa20, error)
func (s *Salsa20) Encrypt(data []byte) ([]byte, error)
func (s *Salsa20) Decrypt(encrypted []byte) ([]byte, error)

func NewAES(salt, key []byte) (*AES, error)
func (a *AES) Encrypt(data []byte) ([]byte, error)
func (a *AES) Decrypt(encrypted []byte) ([]byte, error)

func EncryptToBase64(c Cipher, msg []byte) (string, error)
func DecryptFromBase64(c Cipher, encrypted string) ([]byte, error)

License

crypto-go is licensed under the MIT license. Copyright © 2016-2018 Teambition.

Documentation

Index

Constants

View Source
const Version = "3.0.0"

Version -

Variables

This section is empty.

Functions

func Equal

func Equal(a, b []byte) bool

Equal compares two []byte for equality without leaking timing information.

fmt.Println(crypto.Equal([]byte("123"), []byte("123")))
fmt.Println(crypto.Equal([]byte("123"), []byte("1234")))

func HashSum

func HashSum(h func() hash.Hash, data []byte) []byte

HashSum returns hash result by given hash function and data

fmt.Println(crypto.HashSum(md5.New, []byte("hello")))
fmt.Println(crypto.HashSum(sha256.New, []byte("hello")))

func HmacSum

func HmacSum(h func() hash.Hash, key, data []byte) []byte

HmacSum returns Keyed-Hash result by given hash function, key and data

fmt.Println(crypto.HmacSum(md5.New, []byte("my key"), []byte("hello")))
fmt.Println(crypto.HmacSum(sha256.New, []byte("my key"), []byte("hello")))

func Rand

func Rand(b []byte)

Rand fill rand bytes.

func RandN

func RandN(size int) []byte

RandN return rand bytes by given size.

fmt.Println(crypto.RandN(16))

func SHA256Hmac

func SHA256Hmac(key, data []byte) []byte

SHA256Hmac returns SHA256 Keyed-Hash result by given key and data

fmt.Println(crypto.SHA256Hmac([]byte("my key"), []byte("hello")))

func SHA256Sum

func SHA256Sum(data []byte) []byte

SHA256Sum returns SHA256 hash result by given data

fmt.Println(crypto.SHA256Sum([]byte("hello")))

Types

type Rotating

type Rotating []interface{}

Rotating is used to verify data through a rotating credential system, in which new server keys can be added and old ones removed regularly, without invalidating client credentials.

var err error
var claims josejwt.Claims
index := Rotating(keys).Verify(func(key interface{}) bool {
	if err = jwtToken.Validate(key, method); err == nil {
		claims = jwtToken.Claims()
		return true
	}
	return false
})

func (Rotating) Verify

func (r Rotating) Verify(fn func(interface{}) bool) (index int)

Verify verify with fn and keys, if Verify failure, it return -1, otherwise the index of key.

type RotatingBytes

type RotatingBytes [][]byte

RotatingBytes is similar to Rotating.

func (RotatingBytes) Verify

func (r RotatingBytes) Verify(fn func([]byte) bool) (index int)

Verify verify with fn and keys, if Verify failure, it return -1, otherwise the index of key.

type RotatingStr

type RotatingStr []string

RotatingStr is similar to Rotating.

func (RotatingStr) Verify

func (r RotatingStr) Verify(fn func(string) bool) (index int)

Verify verify with fn and keys, if Verify failure, it return -1, otherwise the index of key.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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