aesgcm

package
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2021 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package aesgcm provides easy to use function to perform encryption and decryption using the secure AES-GCM AHEAD algorithm. It support AES-128, AES-192 and AES-256 key sizes. This package wraps the go's 'crypto/aes' library operations into easy to use functions like 'Encrypt' and 'Decrypt'. It also provides the required constants that are needed for dermining size of keys and nonce. Additionally it ensure a proper cryptographically secure nonce is used for the encryption process automatically when the same is not supplied.

Example
package main

import (
	"crypto/subtle"
	"encoding/hex"
	"fmt"

	"github.com/boseji/auth/aesgcm"
)

func main() {
	// Load your secret key from a safe place and reuse it across multiple
	// Seal/Open calls. (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.
	// When decoded the key should be 16 bytes (AES-128) or 32 (AES-256).
	key, _ := hex.DecodeString("6368616e676520746869732070617373776f726420746f206120736563726574")
	plaintext := []byte("exampleplaintext")

	// Encryption without Nonce, it will be automatically be generated automatically
	ciphertext, nonce, err := aesgcm.Encrypt(plaintext, key, nil)
	if err != nil {
		panic(err.Error())
	}

	// Decryption should use the same nonce and key as used for Encryption
	plaintext2, err := aesgcm.Decrypt(ciphertext, nonce, key)
	if err != nil {
		panic(err.Error())
	}

	// Cryptographically secure constant time comparison
	if subtle.ConstantTimeCompare(plaintext, plaintext2) != 1 {
		fmt.Println("Error results don't match")
		return
	}

	fmt.Println("Success !")

}
Output:
Success !

Index

Examples

Constants

View Source
const (
	// NonceSize provides the default recommended Nonce size
	NonceSize = 12
	// KeySizeAES128 specifies the minimum Key size needed for AES-GCM-128
	KeySizeAES128 = 16
	// KeySizeAES192 specifies the minimum Key size needed for AES-GCM-192
	KeySizeAES192 = 24
	// KeySizeAES256 specifies the minimum Key size needed for AES-GCM-256
	KeySizeAES256 = 32
)

Variables

This section is empty.

Functions

func Decrypt

func Decrypt(ciphertext, nonce, key []byte) (plaintext []byte, err error)

Decrypt function performs the AES-GCM Decryption

Example
package main

import (
	"encoding/hex"
	"fmt"

	"github.com/boseji/auth/aesgcm"
)

func main() {
	// Load your secret key from a safe place and reuse it across multiple
	// Seal/Open calls. (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.
	// When decoded the key should be 16 bytes (AES-128) or 32 (AES-256).
	key, _ := hex.DecodeString("6368616e676520746869732070617373776f726420746f206120736563726574")
	ciphertext, _ := hex.DecodeString("022100c774487456c404b3bb9b3938c7234b3837746a27fbd84a91df5d3ba62e")
	nonce, _ := hex.DecodeString("eed01d5099dc428d44bb18f1")

	// Decryption with supplied nonce and key
	plaintext, err := aesgcm.Decrypt(ciphertext, nonce, key)
	if err != nil {
		panic(err.Error())
	}

	fmt.Printf("Plain Text: %s\n", plaintext)

}
Output:
Plain Text: exampleplaintext

func Encrypt

func Encrypt(plaintext, key, iNonce []byte) (ciphertext []byte, nonce []byte, err error)

Encrypt function performs the AES-GCM Encryption The supplied 'plaintext' is encrypted using the 'key'. Typically a Nonce is needed for the computation involved in encryption. This can be supplied using `iNonce` parameter and should at least have a size equivalent to 'NonceSize' constant. In case no nonce is supplied then a cryptographically secure random nonce is generated (using auth.GetRandom function).

Example
package main

import (
	"encoding/hex"
	"fmt"

	"github.com/boseji/auth/aesgcm"
)

func main() {
	// Load your secret key from a safe place and reuse it across multiple
	// Seal/Open calls. (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.
	// When decoded the key should be 16 bytes (AES-128) or 32 (AES-256).
	key, _ := hex.DecodeString("6368616e676520746869732070617373776f726420746f206120736563726574")
	plaintext := []byte("exampleplaintext")

	// Should not use a Zero Nonce for normal operation
	iNonce := make([]byte, aesgcm.NonceSize)

	// Encryption using a fixed or pre-set nonce
	ciphertext, nonce, err := aesgcm.Encrypt(plaintext, key, iNonce)
	if err != nil {
		panic(err.Error())
	}

	fmt.Printf("Nonce: %x\n", nonce)
	fmt.Printf("Cipher Text: %x\n", ciphertext)

}
Output:
Nonce: 000000000000000000000000
Cipher Text: b9e3743b8019206437b8ddc1ceb150096aa14c85d9f623096ffffaf48232c4f8

Types

This section is empty.

Jump to

Keyboard shortcuts

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