crypto

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2023 License: MIT Imports: 19 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Crypto

Cryptography library with support of MD4, MD5, SHA1, SHA256, SHA256224, SHA512, SHA512224, SHA512256, SHA384, SHA3224, SHA3256, SHA3384, SHA3512, KECCAK256 and KECCAK512 algorithms.

Create New Crypto Driver

Caution: This function generate panic if key is empty!

import "github.com/gomig/crypto"
crp := crypto.NewCryptography("my-secret-key")

Hash Algorithm Type

Crypto driver use HashAlgo type for algorithm params. this type can parsed from string or predefined constant.

import "github.com/gomig/crypto"
var a crypto.HashAlgo
a.Parse("md4")

// Or Set Manually
a = crypto.MD5

Usage

Crypto interface contains following methods:

Hash

Make hash for data.

// Signature:
Hash(data string, algo HashAlgo) (string, error)

// Example:
import "github.com/gomig/crypto"
h, err := crp.Hash("my data", crypto.MD5)
HashFilename

Make hashed filename based on current timestamp.

// Signature:
HashFilename(filename string, algo HashAlgo) (string, error)

// Example:
import "github.com/gomig/crypto"
h, err := crp.HashFilename("myfile.jpg", crypto.MD5) // => a1469c8565fc80b55220324eb3056d3e.jpg
HashSize

Get hash size for algorithm. return -1 if invalid algo passed or on error.

// Signature:
HashSize(algo HashAlgo) int

// Example:
import "github.com/gomig/crypto"
size := crp.HashSize(crypto.MD5) // => 16
Check

Check data against hash.

// Signature:
Check(data string, hash string, algo HashAlgo) (bool, error)

// Example:
import "github.com/gomig/crypto"
ok, err := crp.Check("my-password", "a1469c8565fc80b55220324eb3056d3e", crypto.MD5)
Encrypt

Encrypt data.

Note: This function returns raw byte array, if you need string version of encrypted data use EncryptHEX or EncryptBase64.

// Signature:
Encrypt(data []byte) ([]byte, error)

// Example:
data, err := crp.Encrypt([]byte("my data to enc"))
Decrypt

Decrypt data.

Note: This function accept raw byte array as input. if you need to decrypt Hex or Base64 encoded data use DecryptHex or DecryptBase64.

// Signature:
Decrypt(data []byte) ([]byte, error)

// Example:
data, err := crp.Decrypt(dataBytes)
EncryptHEX

Encrypt data and return encrypted value as hex encoded string.

// Signature:
EncryptHEX(data []byte) (string, error)

// Example:
data, err := crp.EncryptHEX([]byte("my data"))
DecryptHex

Decrypt data from hex encoded string.

// Signature:
DecryptHex(hexString string) ([]byte, error)

// Example:
res, err := crp.DecryptHex(encHexData)
EncryptBase64

Encrypt data and return encrypted value as base64 encoded string.

// Signature:
EncryptBase64(data []byte) (string, error)

// Example:
data, err := crp.EncryptBase64([]byte("my data"))
DecryptBase64

Decrypt data from base64 encoded string.

// Signature:
DecryptBase64(base64String string) ([]byte, error)

// Example
res, err := crp.DecryptBase64(encBase64Data)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Crypto

type Crypto interface {
	// Hash make hash for data
	Hash(data string, algo HashAlgo) (string, error)
	// HashFilename make hashed filename based on current timestamp
	HashFilename(filename string, algo HashAlgo) (string, error)
	// HashSize get hash size for algorithm
	// return -1 if invalid algo passed
	HashSize(algo HashAlgo) int
	// Check check data against hash
	Check(data string, hash string, algo HashAlgo) (bool, error)
	// Encrypt data
	Encrypt(data []byte) ([]byte, error)
	// Decrypt data
	Decrypt(data []byte) ([]byte, error)
	// EncryptHEX encrypt data and return encrypted value as hex encoded string
	EncryptHEX(data []byte) (string, error)
	// DecryptHex decrypt data from hex encoded string.
	DecryptHex(hexString string) ([]byte, error)
	// EncryptBase64 encrypt data and return encrypted value as base64 encoded string
	EncryptBase64(data []byte) (string, error)
	// DecryptBase64 decrypt data from base64 encoded string.
	DecryptBase64(base64String string) ([]byte, error)
}

Crypto cryptography interface

func NewCryptography

func NewCryptography(key string) Crypto

NewCryptography create a new cryptography instance

type HashAlgo

type HashAlgo int

HashAlgo available hash algo list

const (
	// MD4 hash algorithm
	MD4 HashAlgo = iota + 1
	// MD5 hash algorithm
	MD5
	// SHA1 hash algorithm
	SHA1
	// SHA256 hash algorithm
	SHA256
	// SHA256224 hash algorithm
	SHA256224
	// SHA384 hash algorithm
	SHA384
	// SHA512 hash algorithm
	SHA512
	// SHA512224 hash algorithm
	SHA512224
	// SHA512256 hash algorithm
	SHA512256
	// SHA3224 hash algorithm
	SHA3224
	// SHA3256 hash algorithm
	SHA3256
	// SHA3384 hash algorithm
	SHA3384
	// SHA3512 hash algorithm
	SHA3512
	// KECCAK256 hash algorithm
	KECCAK256
	// KECCAK512 hash algorithm
	KECCAK512
)

func (*HashAlgo) Parse

func (a *HashAlgo) Parse(algo string) bool

Parse algo from string

Jump to

Keyboard shortcuts

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