encryption

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: May 1, 2020 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecryptData

func DecryptData(data []byte, key []byte) ([]byte, error)

DecryptData receives a data byte array and a key byte array. The key must be 256 bits (or 32 bytes) large. It will decrypt the data bytes with the AES CBC algorithm with PKCS#5 padding. It also generates an IV. Afterwards you will receive the decrypted byte array or an error.

Example
package main

import (
	"github.com/MatthiasSchild/gobox/encryption"
	"log"
)

func main() {
	// The encrypted data
	enc := []byte{
		0x52, 0x26, 0x3a, 0x69, 0x17, 0x53, 0xe5, 0x7a,
		0xb2, 0xd3, 0x61, 0x24, 0x46, 0xee, 0x80, 0xdd,
		0x81, 0x2a, 0x0b, 0x33, 0x2b, 0xdd, 0x18, 0xaf,
		0x10, 0xc4, 0x02, 0x5d, 0x2d, 0x60, 0xdb, 0x8e,
	}
	// The key for the decryption. Must be 32 bytes long (256 bits)
	key := []byte{
		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
		0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
		0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
		0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
	}

	data, err := encryption.DecryptData(enc, key)
	if err != nil {
		log.Fatal(err)
	}

	log.Println(string(data))
}
Output:

func DecryptDataHex

func DecryptDataHex(dataHex string, key []byte) ([]byte, error)

DecryptDataHex receives a data hex string and a key byte array. The hex string should represent the bytes of the encrypted data. The key must be 256 bits (or 32 bytes) large. It will decrypt the data bytes with the AES CBC algorithm with PKCS#5 padding. It also generates an IV. Afterwards you will receive the decrypted byte array or an error.

Example
package main

import (
	"github.com/MatthiasSchild/gobox/encryption"
	"log"
)

func main() {
	// The encrypted data
	enc := "03e620100d8eefbd3508961ac8eef46ea2826d3d241585de3aad50172ca73f76"
	// The key for the decryption. Must be 32 bytes long (256 bits)
	key := []byte{
		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
		0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
		0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
		0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
	}

	data, err := encryption.DecryptDataHex(enc, key)
	if err != nil {
		log.Fatal(err)
	}

	log.Println(string(data))
}
Output:

func DecryptDataHexPass

func DecryptDataHexPass(dataString string, password string) ([]byte, error)

DecryptDataHexPass receives a data hex string and a password string. The hex string should represent the bytes of the encrypted data. The password will be hashed with SHA-256 and the result will be used as key. It will decrypt the data bytes with the AES CBC algorithm with PKCS#5 padding. It also generates an IV. Afterwards you will receive the decrypted byte array or an error.

Example
package main

import (
	"github.com/MatthiasSchild/gobox/encryption"
	"log"
)

func main() {
	// The encrypted data
	enc := "c065459bed4e6606c7e24cb9e1d979d8a47bad9216c06e9a925884c116da4544"
	// The password for the decryption. The length doesn't matter.
	password := "GolangIsAwes0me!"

	data, err := encryption.DecryptDataHexPass(enc, password)
	if err != nil {
		log.Fatal(err)
	}

	log.Println(string(data))
}
Output:

func DecryptDataPass

func DecryptDataPass(data []byte, password string) ([]byte, error)

DecryptDataPass receives a data byte array and a password string. The password will be hashed with SHA-256 and the result will be used as key. It will decrypt the data bytes with the AES CBC algorithm with PKCS#5 padding. It also generates an IV. Afterwards you will receive the decrypted byte array or an error.

Example
package main

import (
	"github.com/MatthiasSchild/gobox/encryption"
	"log"
)

func main() {
	// The encrypted data
	enc := []byte{
		0xac, 0x81, 0x62, 0x2e, 0xea, 0xf1, 0x9a, 0x9e,
		0xc5, 0x24, 0xab, 0x7b, 0x2d, 0x0e, 0xfa, 0x2e,
		0x03, 0xfc, 0xca, 0x5e, 0xfb, 0x3a, 0xfc, 0xbb,
		0x13, 0x2f, 0xfd, 0xa2, 0xc3, 0xff, 0x59, 0x7c,
	}
	// The password for the decryption. The length doesn't matter.
	password := "GolangIsAwes0me!"

	data, err := encryption.DecryptDataPass(enc, password)
	if err != nil {
		log.Fatal(err)
	}

	log.Println(string(data))
}
Output:

func EncryptData

func EncryptData(data []byte, key []byte) ([]byte, error)

EncryptData receives a data byte array and a key byte array. The key must be 256 bits (or 32 bytes) large. It will encrypt the data bytes with the AES CBC algorithm with PKCS#5 padding. It also generates an IV. Afterwards you will receive one encrypted byte array or an error.

Example
package main

import (
	"github.com/MatthiasSchild/gobox/encryption"
	"log"
)

func main() {
	// The data you want to encrypt
	data := []byte("Hello gopher!")
	// The key for the encryption. Must be 32 bytes long (256 bits)
	key := []byte{
		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
		0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
		0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
		0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
	}

	// enc contains the encrypted data as []byte.
	enc, err := encryption.EncryptData(data, key)
	if err != nil {
		log.Fatal(err)
	}

	log.Printf("% x\n", enc)
}
Output:

func EncryptDataHex

func EncryptDataHex(data []byte, key []byte) (string, error)

EncryptDataHex receives a data byte array and a key byte array. The key must be 256 bits (or 32 bytes) large. It will encrypt the data bytes with the AES CBC algorithm with PKCS#5 padding. It also generates an IV. The encrypted result will be parsed to an hex string. Afterwards you will receive a hex string with the encrypted data or an error.

Example
package main

import (
	"github.com/MatthiasSchild/gobox/encryption"
	"log"
)

func main() {
	// The data you want to encrypt
	data := []byte("Hello gopher!")
	// The key for the encryption. Must be 32 bytes long (256 bits)
	key := []byte{
		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
		0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
		0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
		0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
	}

	// enc contains the encrypted data as string.
	enc, err := encryption.EncryptDataHex(data, key)
	if err != nil {
		log.Fatal(err)
	}

	log.Printf("%s\n", enc)
}
Output:

func EncryptDataHexPass

func EncryptDataHexPass(data []byte, password string) (string, error)

EncryptDataHexPass receives a data byte array and a password string. The password will be hashed with SHA-256 and the result will be used as key. It will encrypt the data bytes with the AES CBC algorithm with PKCS#5 padding. It also generates an IV. The encrypted result will be parsed to an hex string. Afterwards you will receive a hex string with the encrypted data or an error.

Example
package main

import (
	"github.com/MatthiasSchild/gobox/encryption"
	"log"
)

func main() {
	// The data you want to encrypt
	data := []byte("Hello gopher!")
	// The password for the encryption. The length doesn't matter.
	password := "GolangIsAwes0me!"

	// enc contains the encrypted data as string.
	enc, err := encryption.EncryptDataHexPass(data, password)
	if err != nil {
		log.Fatal(err)
	}

	log.Printf("%s\n", enc)
}
Output:

func EncryptDataPass

func EncryptDataPass(data []byte, password string) ([]byte, error)

EncryptDataPass receives a data byte array and a password string. The password will be hashed with SHA-256 and the result will be used as key. It will encrypt the data bytes with the AES CBC algorithm with PKCS#5 padding. It also generates an IV. Afterwards you will receive one encrypted byte array or an error.

Example
package main

import (
	"github.com/MatthiasSchild/gobox/encryption"
	"log"
)

func main() {
	// The data you want to encrypt
	data := []byte("Hello gopher!")
	// The password for the encryption. The length doesn't matter.
	password := "GolangIsAwes0me!"

	// enc contains the encrypted data as []byte.
	enc, err := encryption.EncryptDataPass(data, password)
	if err != nil {
		log.Fatal(err)
	}

	log.Printf("% x\n", enc)
}
Output:

func PasswordToKey

func PasswordToKey(password string) []byte

This method takes a password string and generates the sha256 byte array for using it as 256 bit encryption key

Types

This section is empty.

Jump to

Keyboard shortcuts

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