Cryptography library with support of MD4, MD5, SHA1, SHA256, SHA256224, SHA512, SHA512224, SHA512256, SHA384, SHA3224, SHA3256, SHA3384, SHA3512, KECCAK256 and KECCAK512 algorithms.
Caution: This function generate panic if key is empty!
import "github.com/gomig/crypto"
crp := crypto.NewCryptography("my-secret-key")
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
Crypto interface contains following methods:
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)
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
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 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 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 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)
Encrypt data and return encrypted value as hex encoded string.
// Signature:
EncryptHEX(data []byte) (string, error)
// Example:
data, err := crp.EncryptHEX([]byte("my data"))
Decrypt data from hex encoded string.
// Signature:
DecryptHex(hexString string) ([]byte, error)
// Example:
res, err := crp.DecryptHex(encHexData)
Encrypt data and return encrypted value as base64 encoded string.
// Signature:
EncryptBase64(data []byte) (string, error)
// Example:
data, err := crp.EncryptBase64([]byte("my data"))
Decrypt data from base64 encoded string.
// Signature:
DecryptBase64(base64String string) ([]byte, error)
// Example
res, err := crp.DecryptBase64(encBase64Data)
ISC License
Copyright 2023 @mekramy (github.com/mekramy)
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.