cryptoutils

package module
v0.0.0-...-fe22438 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2015 License: MIT Imports: 5 Imported by: 0

README

go-cryptoutils

Go crypto utils.

GoDoc

Install

go get github.com/koofr/go-cryptoutils

Testing

go get -t
go test

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BetterCTR

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

func NewBetterCTR

func NewBetterCTR(block cipher.Block, iv []byte) *BetterCTR

NewBetterCTR returns a Stream which encrypts/decrypts using the given Block in counter mode. The length of iv must be the same as the Block's block size.

Example
key := []byte("example key 1234")
plaintext := []byte("some plaintext")

block, err := aes.NewCipher(key)
if err != nil {
	panic(err)
}

// The IV needs to be unique, but not secure. Therefore it's common to
// include it at the beginning of the ciphertext.
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
	panic(err)
}

stream := cryptoutils.NewBetterCTR(block, iv)
stream.XORKeyStream(ciphertext[aes.BlockSize:aes.BlockSize+4], plaintext[:4])

state := stream.GetState()

anotherStream := cryptoutils.NewBetterCTRFromState(block, state)
anotherStream.XORKeyStream(ciphertext[aes.BlockSize+4:], plaintext[4:])

// It's important to remember that ciphertexts must be authenticated
// (i.e. by using crypto/hmac) as well as being encrypted in order to
// be secure.

// CTR mode is the same for both encryption and decryption, so we can
// also decrypt that ciphertext with NewBetterCTR.

plaintext2 := make([]byte, len(plaintext))
stream = cryptoutils.NewBetterCTR(block, iv)
stream.XORKeyStream(plaintext2, ciphertext[aes.BlockSize:])

fmt.Printf("%s\n", plaintext2)
Output:

some plaintext

func NewBetterCTRFromState

func NewBetterCTRFromState(block cipher.Block, state []byte) *BetterCTR

NewBetterCTR returns a Stream from state

func (*BetterCTR) GetState

func (x *BetterCTR) GetState() []byte

func (*BetterCTR) SetState

func (x *BetterCTR) SetState(state []byte) error

func (*BetterCTR) XORKeyStream

func (x *BetterCTR) XORKeyStream(dst, src []byte)

Directories

Path Synopsis
Package bettermd5 implements the MD5 hash algorithm as defined in RFC 1321.
Package bettermd5 implements the MD5 hash algorithm as defined in RFC 1321.

Jump to

Keyboard shortcuts

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