dongle

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2021 License: MIT Imports: 14 Imported by: 0

README

dongle

Carbon Release Go Build Go Report Card codecov Go doc License

English | 简体中文

Introduction

A simple, semantic and developer-friendly golang package for encoding&decoding and encryption&decryption

Dongle has been included by awesome-go, if you think it is helpful, please give me a star

github.com/golang-module/dongle

gitee.com/go-package/dongle

Installation
// By github
go get -u github.com/golang-module/dongle

import (
    "github.com/golang-module/dongle"
)

// By gitee
go get -u gitee.com/go-package/dongle

import (
    "gitee.com/go-package/dongle"
)
Encode and encrypt
Encode by base32
// Encode by base32 from string and output string
dongle.Encode.FromString("hello world").ByBase32().ToString() // NBSWY3DPEB3W64TMMQ======
// Encode by base32 from string and output byte slice
dongle.Encode.FromString("hello world").ByBase32().ToBytes() // []byte("NBSWY3DPEB3W64TMMQ======")

// Encode by base32 from byte slice and output string
dongle.Encode.FromBytes([]byte("hello world")).ByBase32().ToString() // NBSWY3DPEB3W64TMMQ======
// Encode by base32 from byte slice and output byte slice
dongle.Encode.FromBytes([]byte("hello world")).ByBase32().ToBytes() // []byte("NBSWY3DPEB3W64TMMQ======")
Encode by Base58
// Encode by base58 from string and output string
dongle.Encode.FromString("hello world").ByBase58().ToString() // StV1DL6CwTryKyV
// Encode by base58 from string and output byte slice
dongle.Encode.FromString("hello world").ByBase58().ToBytes() // []byte("StV1DL6CwTryKyV")

// Encode by base58 from byte slice and output string
dongle.Encode.FromBytes([]byte("hello world")).ByBase58().ToString() // StV1DL6CwTryKyV
// Encode by base58 from byte slice and output byte slice
dongle.Encode.FromBytes([]byte("hello world")).ByBase58().ToBytes() // []byte("StV1DL6CwTryKyV")
Encode by base64
// Encode by base64 from string and output string
dongle.Encode.FromString("hello world").ByBase64().ToString() // aGVsbG8gd29ybGQ=
// Encode by base64 from string and output byte slice
dongle.Encode.FromString("hello world").ByBase64().ToBytes() // []byte("aGVsbG8gd29ybGQ=")

// Encode by base64 from byte slice and output string
dongle.Encode.FromBytes([]byte("hello world")).ByBase64().ToString() // aGVsbG8gd29ybGQ=
// Encode by base64 from byte slice and output byte slice
dongle.Encode.FromBytes([]byte("hello world")).ByBase64().ToBytes() // []byte("aGVsbG8gd29ybGQ=")
Encode by hex
// Encode by hex from string and output string
dongle.Encode.FromString("hello world").ByHex().ToString() // 68656c6c6f20776f726c64=
// Encode by hex from string and output byte slice
dongle.Encode.FromString("hello world").ByHex().ToBytes() // []byte("68656c6c6f20776f726c64")

// Encode by hex from byte slice and output string
dongle.Encode.FromBytes([]byte("hello world")).ByHex().ToString() // 68656c6c6f20776f726c64
// Encode by hex from byte slice and output byte slice
dongle.Encode.FromBytes([]byte("hello world")).ByHex().ToBytes() // []byte("68656c6c6f20776f726c64")
Encrypt by md5
// Encrypt by md5 from string and output string with hex encoding
dongle.Encrypt.FromString("hello world").ByMd5().ToString() // 5eb63bbbe01eeed093cb22bb8f5acdc3
dongle.Encrypt.FromString("hello world").ByMd5().ToString("hex") // 5eb63bbbe01eeed093cb22bb8f5acdc3
// Encrypt by md5 from string and output string with base32 encoding
dongle.Encrypt.FromString("hello world").ByMd5().ToString("base32") // L23DXO7AD3XNBE6LEK5Y6WWNYM======
// Encrypt by md5 from string and output string with base64 encoding
dongle.Encrypt.FromString("hello world").ByMd5().ToString("base64") // XrY7u+Ae7tCTyyK7j1rNww==

// Encrypt by md5 from string and output byte slice with hex encoding
dongle.Encrypt.FromString("hello world").ByMd5().ToBytes() // []byte("5eb63bbbe01eeed093cb22bb8f5acdc3")
dongle.Encrypt.FromString("hello world").ByMd5().ToBytes("hex") // []byte("5eb63bbbe01eeed093cb22bb8f5acdc3")
// Encrypt by md5 from string and output byte slice with base32 encoding
dongle.Encrypt.FromString("hello world").ByMd5().ToBytes("base32") // []byte("L23DXO7AD3XNBE6LEK5Y6WWNYM======")
// Encrypt by md5 from string and output byte slice with base64 encoding
dongle.Encrypt.FromString("hello world").ByMd5().ToBytes("base64") // []byte("XrY7u+Ae7tCTyyK7j1rNww==")

// Encrypt by md5 from byte slice and output string with hex encoding
dongle.Encrypt.FromBytes([]byte("hello world")).ByMd5().ToString() // 5eb63bbbe01eeed093cb22bb8f5acdc3
dongle.Encrypt.FromBytes([]byte("hello world")).ByMd5().ToString("hex") // 5eb63bbbe01eeed093cb22bb8f5acdc3
// Encrypt by md5 from byte slice and output string with base32 encoding
dongle.Encrypt.FromBytes([]byte("hello world")).ByMd5().ToString("base32") // L23DXO7AD3XNBE6LEK5Y6WWNYM======
// Encrypt by md5 from byte slice and output string with base64 encoding
dongle.Encrypt.FromBytes([]byte("hello world")).ByMd5().ToString("base64") // XrY7u+Ae7tCTyyK7j1rNww==

// Encrypt by md5 from byte slice and output byte slice with hex encoding
dongle.Encrypt.FromBytes([]byte("hello world")).ByMd5().ToBytes() // []byte("5eb63bbbe01eeed093cb22bb8f5acdc3")
dongle.Encrypt.FromBytes([]byte("hello world")).ByMd5().ToBytes("hex") // []byte("5eb63bbbe01eeed093cb22bb8f5acdc3")
// Encrypt by md5 from byte slice and output byte slice with base32 encoding
dongle.Encrypt.FromBytes([]byte("hello world")).ByMd5().ToBytes("base32") // []byte("L23DXO7AD3XNBE6LEK5Y6WWNYM======")
// Encrypt by md5 from byte slice and output byte slice with base64 encoding
dongle.Encrypt.FromBytes([]byte("hello world")).ByMd5().ToBytes("base64") // []byte("XrY7u+Ae7tCTyyK7j1rNww==")

// Encrypt by md5 from file and output string with hex encoding
dongle.Encrypt.FromFile("./LICENSE")).ByMd5().ToString() // 014f03f9025ea81a8a0e9734be540c53
dongle.Encrypt.FromFile("./LICENSE")).ByMd5().ToString("hex") // 014f03f9025ea81a8a0e9734be540c53
// Encrypt by md5 from file and output string with base32 encoding
dongle.Encrypt.FromFile("./LICENSE")).ByMd5().ToString("base32") // AFHQH6ICL2UBVCQOS42L4VAMKM======
// Encrypt by md5 from file and output string with base64 encoding
dongle.Encrypt.FromFile("./LICENSE")).ByMd5().ToString("base64") // AU8D+QJeqBqKDpc0vlQMUw==

// Encrypt by md5 from file and output byte slice with hex encoding
dongle.Encrypt.FromFile("./LICENSE").ByMd5().ToBytes() // []byte("014f03f9025ea81a8a0e9734be540c53")
dongle.Encrypt.FromFile("./LICENSE").ByMd5().ToBytes("hex") // []byte("014f03f9025ea81a8a0e9734be540c53")
// Encrypt by md5 from file and output byte slice with base32 encoding

dongle.Encrypt.FromFile("./LICENSE").ByMd5().ToBytes("base32") // []byte("AFHQH6ICL2UBVCQOS42L4VAMKM======")
// Encrypt by md5 from file and output byte slice with base64 encoding
dongle.Encrypt.FromFile("./LICENSE").ByMd5().ToBytes("base64") // []byte("AU8D+QJeqBqKDpc0vlQMUw==")
Encrypt by sha1
// Encrypt by sha1 from string and output string with hex encoding
dongle.Encrypt.FromString("hello world").BySha1().ToString() // 2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
dongle.Encrypt.FromString("hello world").BySha1().ToString("hex") // 2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
// Encrypt by sha1 from string and output string with base32 encoding
dongle.Encrypt.FromString("hello world").BySha1().ToString("base32") // FKXGYNOJJ7H3IFO35FPUBC445EPOQRXN
// Encrypt by sha1 from string and output string with base64 encoding
dongle.Encrypt.FromString("hello world").BySha1().ToString("base64") // Kq5sNclPz7QV2+lfQIuc6R7oRu0=

// Encrypt by sha1 from string and output byte slice with hex encoding
dongle.Encrypt.FromString("hello world").BySha1().ToBytes() // []byte("2aae6c35c94fcfb415dbe95f408b9ce91ee846ed")
dongle.Encrypt.FromString("hello world").BySha1().ToBytes("hex") // []byte("2aae6c35c94fcfb415dbe95f408b9ce91ee846ed")
// Encrypt by sha1 from string and output byte slice with base32 encoding
dongle.Encrypt.FromString("hello world").BySha1().ToBytes("base32") // []byte("FKXGYNOJJ7H3IFO35FPUBC445EPOQRXN")
// Encrypt by sha1 from string and output byte slice with base64 encoding
dongle.Encrypt.FromString("hello world").BySha1().ToBytes("base64") // []byte("Kq5sNclPz7QV2+lfQIuc6R7oRu0=")

// Encrypt by sha1 from byte slice and output string with hex encoding
dongle.Encrypt.FromBytes([]byte("hello world")).BySha1().ToString() // 2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
dongle.Encrypt.FromBytes([]byte("hello world")).BySha1().ToString("hex") // 2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
// Encrypt by sha1 from byte slice and output string with base32 encoding
dongle.Encrypt.FromBytes([]byte("hello world")).BySha1().ToString("base32") // FKXGYNOJJ7H3IFO35FPUBC445EPOQRXN
// Encrypt by sha1 from byte slice and output string with base64 encoding
dongle.Encrypt.FromBytes([]byte("hello world")).BySha1().ToString("base64") // Kq5sNclPz7QV2+lfQIuc6R7oRu0=

// Encrypt by sha1 from byte slice and output byte slice with hex encoding
dongle.Encrypt.FromBytes([]byte("hello world")).BySha1().ToBytes() // []byte("2aae6c35c94fcfb415dbe95f408b9ce91ee846ed")
dongle.Encrypt.FromBytes([]byte("hello world")).BySha1().ToBytes("hex") // []byte("2aae6c35c94fcfb415dbe95f408b9ce91ee846ed")
// Encrypt by sha1 from byte slice and output byte slice with base32 encoding
dongle.Encrypt.FromBytes([]byte("hello world")).BySha1().ToBytes("base32") // []byte("FKXGYNOJJ7H3IFO35FPUBC445EPOQRXN")
// Encrypt by sha1 from byte slice and output byte slice with base64 encoding
dongle.Encrypt.FromBytes([]byte("hello world")).BySha1().ToBytes("base64") // []byte("Kq5sNclPz7QV2+lfQIuc6R7oRu0=")
Encrypt by sha224
// Encrypt by sha224 from string and output string with hex encoding
dongle.Encrypt.FromString("hello world").BySha224().ToString() // 2f05477fc24bb4faefd86517156dafdecec45b8ad3cf2522a563582b
dongle.Encrypt.FromString("hello world").BySha224().ToString("hex") // 2f05477fc24bb4faefd86517156dafdecec45b8ad3cf2522a563582b
// Encrypt by sha224 from string and output string with base32 encoding
dongle.Encrypt.FromString("hello world").BySha224().ToString("base32") // F4CUO76CJO2PV36YMULRK3NP33HMIW4K2PHSKIVFMNMCW===
// Encrypt by sha224 from string and output string with base64 encoding
dongle.Encrypt.FromString("hello world").BySha224().ToString("base64") // LwVHf8JLtPrv2GUXFW2v3s7EW4rTzyUipWNYKw==

// Encrypt by sha224 from string and output byte slice with hex encoding
dongle.Encrypt.FromString("hello world").BySha224().ToBytes() // []byte("2f05477fc24bb4faefd86517156dafdecec45b8ad3cf2522a563582b")
dongle.Encrypt.FromString("hello world").BySha224().ToBytes("hex") // []byte("2f05477fc24bb4faefd86517156dafdecec45b8ad3cf2522a563582b")
// Encrypt by sha224 from string and output byte slice with base32 encoding
dongle.Encrypt.FromString("hello world").BySha224().ToBytes("base32") // []byte("F4CUO76CJO2PV36YMULRK3NP33HMIW4K2PHSKIVFMNMCW===")
// Encrypt by sha224 from string and output byte slice with base64 encoding
dongle.Encrypt.FromString("hello world").BySha224().ToBytes("base64") // []byte("LwVHf8JLtPrv2GUXFW2v3s7EW4rTzyUipWNYKw==")

// Encrypt by sha224 from byte slice and output string with hex encoding
dongle.Encrypt.FromBytes([]byte("hello world")).BySha224().ToString() // 2f05477fc24bb4faefd86517156dafdecec45b8ad3cf2522a563582b
dongle.Encrypt.FromBytes([]byte("hello world")).BySha224().ToString("hex") // 2f05477fc24bb4faefd86517156dafdecec45b8ad3cf2522a563582b
// Encrypt by sha224 from byte slice and output string with base32 encoding
dongle.Encrypt.FromBytes([]byte("hello world")).BySha224().ToString("base32") // F4CUO76CJO2PV36YMULRK3NP33HMIW4K2PHSKIVFMNMCW===
// Encrypt by sha224 from byte slice and output string with base64 encoding
dongle.Encrypt.FromBytes([]byte("hello world")).BySha224().ToString("base64") // LwVHf8JLtPrv2GUXFW2v3s7EW4rTzyUipWNYKw==

// Encrypt by sha224 from byte slice and output byte slice with hex encoding
dongle.Encrypt.FromBytes([]byte("hello world")).BySha224().ToBytes() // []byte("2f05477fc24bb4faefd86517156dafdecec45b8ad3cf2522a563582b")
dongle.Encrypt.FromBytes([]byte("hello world")).BySha224().ToBytes("hex") // []byte("2f05477fc24bb4faefd86517156dafdecec45b8ad3cf2522a563582b")
// Encrypt by sha224 from byte slice and output byte slice with base32 encoding
dongle.Encrypt.FromBytes([]byte("hello world")).BySha224().ToBytes("base32") // []byte("F4CUO76CJO2PV36YMULRK3NP33HMIW4K2PHSKIVFMNMCW===")
// Encrypt by sha224 from byte slice and output byte slice with base64 encoding
dongle.Encrypt.FromBytes([]byte("hello world")).BySha224().ToBytes("base64") // []byte("LwVHf8JLtPrv2GUXFW2v3s7EW4rTzyUipWNYKw==")
Encrypt by sha256
// Encrypt by sha256 from string and output string with hex encoding
dongle.Encrypt.FromString("hello world").BySha256().ToString() // b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
dongle.Encrypt.FromString("hello world").BySha256().ToString("hex") // b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
// Encrypt by sha256 from string and output string with base32 encoding
dongle.Encrypt.FromString("hello world").BySha256().ToString("base32") // XFGSPOMTJU7ARJJOKLL5U7NL7LCIJ37DPJJYB3UQRD32ZYXPZXUQ====
// Encrypt by sha256 from string and output string with base64 encoding
dongle.Encrypt.FromString("hello world").BySha256().ToString("base64") // uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek=

// Encrypt by sha256 from string and output byte slice with hex encoding
dongle.Encrypt.FromString("hello world").BySha256().ToBytes() // []byte("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9")
dongle.Encrypt.FromString("hello world").BySha256().ToBytes("hex") // []byte("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9")
// Encrypt by sha256 from string and output byte slice with base32 encoding
dongle.Encrypt.FromString("hello world").BySha256().ToBytes("base32") // []byte("XFGSPOMTJU7ARJJOKLL5U7NL7LCIJ37DPJJYB3UQRD32ZYXPZXUQ====")
// Encrypt by sha256 from string and output byte slice with base64 encoding
dongle.Encrypt.FromString("hello world").BySha256().ToBytes("base64") // []byte("uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek=")

// Encrypt by sha256 from byte slice and output string with hex encoding
dongle.Encrypt.FromBytes([]byte("hello world")).BySha256().ToString() // b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
dongle.Encrypt.FromBytes([]byte("hello world")).BySha256().ToString("hex") // b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
// Encrypt by sha256 from byte slice and output string with base32 encoding
dongle.Encrypt.FromBytes([]byte("hello world")).BySha256().ToString("base32") // XFGSPOMTJU7ARJJOKLL5U7NL7LCIJ37DPJJYB3UQRD32ZYXPZXUQ====
// Encrypt by sha256 from byte slice and output string with base64 encoding
dongle.Encrypt.FromBytes([]byte("hello world")).BySha256().ToString("base64") // uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek=

// Encrypt by sha256 from byte slice and output byte slice with hex encoding
dongle.Encrypt.FromBytes([]byte("hello world")).BySha256().ToBytes() // []byte("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9")
dongle.Encrypt.FromBytes([]byte("hello world")).BySha256().ToBytes("hex") // []byte("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9")
// Encrypt by sha256 from byte slice and output byte slice with base32 encoding
dongle.Encrypt.FromBytes([]byte("hello world")).BySha256().ToBytes("base32") // []byte("XFGSPOMTJU7ARJJOKLL5U7NL7LCIJ37DPJJYB3UQRD32ZYXPZXUQ====")
// Encrypt by sha256 from byte slice and output byte slice with base64 encoding
dongle.Encrypt.FromBytes([]byte("hello world")).BySha256().ToBytes("base64") // []byte("uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek=")
Encrypt by sha384
// Encrypt by sha384 from string and output string with hex encoding
dongle.Encrypt.FromString("hello world").BySha384().ToString() // fdbd8e75a67f29f701a4e040385e2e23986303ea10239211af907fcbb83578b3e417cb71ce646efd0819dd8c088de1bd
dongle.Encrypt.FromString("hello world").BySha384().ToString("hex") // fdbd8e75a67f29f701a4e040385e2e23986303ea10239211af907fcbb83578b3e417cb71ce646efd0819dd8c088de1bd
// Encrypt by sha384 from string and output string with base32 encoding
dongle.Encrypt.FromString("hello world").BySha384().ToString("base32") // 7W6Y45NGP4U7OANE4BADQXROEOMGGA7KCARZEENPSB74XOBVPCZ6IF6LOHHGI3X5BAM53DAIRXQ32===
// Encrypt by sha384 from string and output string with base64 encoding
dongle.Encrypt.FromString("hello world").BySha384().ToString("base64") // /b2OdaZ/KfcBpOBAOF4uI5hjA+oQI5IRr5B/y7g1eLPkF8txzmRu/QgZ3YwIjeG9

// Encrypt by sha384 from string and output byte slice with hex encoding
dongle.Encrypt.FromString("hello world").BySha384().ToBytes() // []byte("fdbd8e75a67f29f701a4e040385e2e23986303ea10239211af907fcbb83578b3e417cb71ce646efd0819dd8c088de1bd")
dongle.Encrypt.FromString("hello world").BySha384().ToBytes("hex") // []byte("fdbd8e75a67f29f701a4e040385e2e23986303ea10239211af907fcbb83578b3e417cb71ce646efd0819dd8c088de1bd")
// Encrypt by sha384 from string and output byte slice with base32 encoding
dongle.Encrypt.FromString("hello world").BySha384().ToBytes("base32") // []byte("7W6Y45NGP4U7OANE4BADQXROEOMGGA7KCARZEENPSB74XOBVPCZ6IF6LOHHGI3X5BAM53DAIRXQ32===")
// Encrypt by sha384 from string and output byte slice with base64 encoding
dongle.Encrypt.FromString("hello world").BySha384().ToBytes("base64") // []byte("/b2OdaZ/KfcBpOBAOF4uI5hjA+oQI5IRr5B/y7g1eLPkF8txzmRu/QgZ3YwIjeG9")

// Encrypt by sha384 from byte slice and output string with hex encoding
dongle.Encrypt.FromBytes([]byte("hello world")).BySha384().ToString() // fdbd8e75a67f29f701a4e040385e2e23986303ea10239211af907fcbb83578b3e417cb71ce646efd0819dd8c088de1bd
dongle.Encrypt.FromBytes([]byte("hello world")).BySha384().ToString("hex") // fdbd8e75a67f29f701a4e040385e2e23986303ea10239211af907fcbb83578b3e417cb71ce646efd0819dd8c088de1bd
// Encrypt by sha384 from byte slice and output string with base32 encoding
dongle.Encrypt.FromBytes([]byte("hello world")).BySha384().ToString("base32") // 7W6Y45NGP4U7OANE4BADQXROEOMGGA7KCARZEENPSB74XOBVPCZ6IF6LOHHGI3X5BAM53DAIRXQ32===
// Encrypt by sha384 from byte slice and output string with base64 encoding
dongle.Encrypt.FromBytes([]byte("hello world")).BySha384().ToString("base64") // /b2OdaZ/KfcBpOBAOF4uI5hjA+oQI5IRr5B/y7g1eLPkF8txzmRu/QgZ3YwIjeG9

// Encrypt by sha384 from byte slice and output byte slice with hex encoding
dongle.Encrypt.FromBytes([]byte("hello world")).BySha384().ToBytes() // []byte("fdbd8e75a67f29f701a4e040385e2e23986303ea10239211af907fcbb83578b3e417cb71ce646efd0819dd8c088de1bd")
dongle.Encrypt.FromBytes([]byte("hello world")).BySha384().ToBytes("hex") // []byte("fdbd8e75a67f29f701a4e040385e2e23986303ea10239211af907fcbb83578b3e417cb71ce646efd0819dd8c088de1bd")
// Encrypt by sha384 from byte slice and output byte slice with base32 encoding
dongle.Encrypt.FromBytes([]byte("hello world")).BySha384().ToBytes("base32") // []byte("7W6Y45NGP4U7OANE4BADQXROEOMGGA7KCARZEENPSB74XOBVPCZ6IF6LOHHGI3X5BAM53DAIRXQ32===")
// Encrypt by sha384 from byte slice and output byte slice with base64 encoding
dongle.Encrypt.FromBytes([]byte("hello world")).BySha384().ToBytes("base64") // []byte("/b2OdaZ/KfcBpOBAOF4uI5hjA+oQI5IRr5B/y7g1eLPkF8txzmRu/QgZ3YwIjeG9")
Encrypt by sha512
// Encrypt by sha512 from string and output string with hex encoding
dongle.Encrypt.FromString("hello world").BySha512().ToString() // 309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f
dongle.Encrypt.FromString("hello world").BySha512().ToString("hex") // 309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f
// Encrypt by sha512 from string and output string with base32 encoding
dongle.Encrypt.FromString("hello world").BySha512().ToString("base32") // GCPMYSE4CLLOWTGEB5IMSAXSWTIO257OKENHY6U3ZU6KQ3KM3BXZRHOTLPC76SMWODNDIJK3IWYM7WBQ5APWAXOPPXCVILUTV2ONO3Y=
// Encrypt by sha512 from string and output string with base64 encoding
dongle.Encrypt.FromString("hello world").BySha512().ToString("base64") // MJ7MSJwS1utMxA9QyQLytNDtd+5RGnx6m808qG1M2G+YndNbxf9JlnDaNCVbRbDP2DDoH2Bdz33FVC6TrpzXbw==

// Encrypt by sha512 from string and output byte slice with hex encoding
dongle.Encrypt.FromString("hello world").BySha512().ToBytes() // []byte("309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f")
dongle.Encrypt.FromString("hello world").BySha512().ToBytes("hex") // []byte("309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f")
// Encrypt by sha512 from string and output byte slice with base32 encoding
dongle.Encrypt.FromString("hello world").BySha512().ToBytes("base32") // []byte("GCPMYSE4CLLOWTGEB5IMSAXSWTIO257OKENHY6U3ZU6KQ3KM3BXZRHOTLPC76SMWODNDIJK3IWYM7WBQ5APWAXOPPXCVILUTV2ONO3Y=")
// Encrypt by sha512 from string and output byte slice with base64 encoding
dongle.Encrypt.FromString("hello world").BySha512().ToBytes("base64") // []byte("MJ7MSJwS1utMxA9QyQLytNDtd+5RGnx6m808qG1M2G+YndNbxf9JlnDaNCVbRbDP2DDoH2Bdz33FVC6TrpzXbw==")

// Encrypt by sha512 from byte slice and output string with hex encoding
dongle.Encrypt.FromBytes([]byte("hello world")).BySha512().ToString() // 309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f
dongle.Encrypt.FromBytes([]byte("hello world")).BySha512().ToString("hex") // 309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f
// Encrypt by sha512 from byte slice and output string with base32 encoding
dongle.Encrypt.FromBytes([]byte("hello world")).BySha512().ToString("base32") // GCPMYSE4CLLOWTGEB5IMSAXSWTIO257OKENHY6U3ZU6KQ3KM3BXZRHOTLPC76SMWODNDIJK3IWYM7WBQ5APWAXOPPXCVILUTV2ONO3Y=
// Encrypt by sha512 from byte slice and output string with base64 encoding
dongle.Encrypt.FromBytes([]byte("hello world")).BySha512().ToString("base64") // MJ7MSJwS1utMxA9QyQLytNDtd+5RGnx6m808qG1M2G+YndNbxf9JlnDaNCVbRbDP2DDoH2Bdz33FVC6TrpzXbw==

// Encrypt by sha512 from byte slice and output byte slice with hex encoding
dongle.Encrypt.FromBytes([]byte("hello world")).BySha512().ToBytes() // []byte("309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f")
dongle.Encrypt.FromBytes([]byte("hello world")).BySha512().ToBytes("hex") // []byte("309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f")
// Encrypt by sha512 from byte slice and output byte slice with base32 encoding
dongle.Encrypt.FromBytes([]byte("hello world")).BySha512().ToBytes("base32") // []byte("GCPMYSE4CLLOWTGEB5IMSAXSWTIO257OKENHY6U3ZU6KQ3KM3BXZRHOTLPC76SMWODNDIJK3IWYM7WBQ5APWAXOPPXCVILUTV2ONO3Y=")
// Encrypt by sha512 from byte slice and output byte slice with base64 encoding
dongle.Encrypt.FromBytes([]byte("hello world")).BySha512().ToBytes("base64") // []byte("MJ7MSJwS1utMxA9QyQLytNDtd+5RGnx6m808qG1M2G+YndNbxf9JlnDaNCVbRbDP2DDoH2Bdz33FVC6TrpzXbw==")
Encrypt by aes
cipher := NewCipher()

// Encrypt by AES/CBC/PKCS7Padding algorithm from string and output string with hex encoding
cipher.SetMode(dongle.CBC)
cipher.SetPadding(dongle.PKCS7Padding)
cipher.SetKey("0123456789abcdef")
cipher.SetIV("0123456789abcdef")
dongle.Encrypt.FromString("hello world").ByAes(cipher).ToString() // c1e9b4529aac9793010f4677f6358efe
dongle.Encrypt.FromString("hello world").ByAes(cipher).ToString("hex") // c1e9b4529aac9793010f4677f6358efe
Decode and decrypt
Decode by base32
// Decode by base32 from string and output string
dongle.Decode.FromString("NBSWY3DPEB3W64TMMQ======").ByBase32().ToString() // hello world
// Decode by base32 from string and output byte slice
dongle.Decode.FromString("NBSWY3DPEB3W64TMMQ======").ByBase32().ToBytes() // []byte("hello world")

// Decode by base32 from byte slice and output string
dongle.Decode.FromBytes([]byte("NBSWY3DPEB3W64TMMQ======")).ByBase32().ToString() // hello world
// Decode by base32 from byte slice and output byte slice
dongle.Decode.FromBytes([]byte("NBSWY3DPEB3W64TMMQ======")).ByBase32().ToBytes() // []byte("hello world")
Decode by base58
// Decode by base58 from string and output string
dongle.Decode.FromString("StV1DL6CwTryKyV").ByBase58().ToString() // hello world
// Decode by base58 from string and output byte slice
dongle.Decode.FromString("StV1DL6CwTryKyV").ByBase58().ToBytes() // []byte("hello world")

// Decode by base58 from byte slice and output string
dongle.Decode.FromBytes([]byte("StV1DL6CwTryKyV")).ByBase58().ToString() // hello world
// Decode by base58 from byte slice and output byte slice
dongle.Decode.FromBytes([]byte("StV1DL6CwTryKyV")).ByBase58().ToBytes() // []byte("hello world")
Decode by base64
// Decode by base64 from string and output string
dongle.Decode.FromString("aGVsbG8gd29ybGQ=").ByBase64().ToString() // hello world
// Decode by base64 from string and output byte slice
dongle.Decode.FromString("aGVsbG8gd29ybGQ=").ByBase64().ToBytes() // []byte("hello world")

// Decode by base64 from byte slice and output string
dongle.Decode.FromBytes([]byte("aGVsbG8gd29ybGQ=")).ByBase64().ToString() // hello world
// Decode by base64 from byte slice and output byte slice
dongle.Decode.FromBytes([]byte("aGVsbG8gd29ybGQ=")).ByBase64().ToBytes() // []byte("hello world")
Decode by hex
// Decode by hex from string and output string
dongle.Decode.FromString("68656c6c6f20776f726c64").ByHex().ToString() // hello world
// Decode by hex from string and output byte slice
dongle.Decode.FromString("68656c6c6f20776f726c64").ByHex().ToBytes() // []byte("hello world")

// Decode by hex from byte slice and output string
dongle.Decode.FromBytes([]byte("68656c6c6f20776f726c64")).ByHex().ToString() // hello world
// Decode by hex from byte slice and output byte slice
dongle.Decode.FromBytes([]byte("68656c6c6f20776f726c64")).ByHex().ToBytes() // []byte("hello world")
Decrypt by aes
cipher := NewCipher()

// Decrypt by AES/CBC/PKCS7Padding algorithm from string with hex decoding and output string
cipher.SetMode(dongle.CBC)
cipher.SetPadding(dongle.PKCS7Padding)
cipher.SetKey("0123456789abcdef")
cipher.SetIV("0123456789abcdef")
dongle.Encrypt.FromString("c1e9b4529aac9793010f4677f6358efe").ByAes(cipher).ToString() // hello world
dongle.Encrypt.FromString("c1e9b4529aac9793010f4677f6358efe", "hex").ByAes(cipher).ToString() // hello world
Error handling

If more than one error occurs, only the first error is returned

e := dongle.Encrypy.FromFile("./demo.txt").ByMd5()
if e.Error != nil {
    // Error handle...
    log.Fatal(c.Error)
}
fmt.Println(c.ToString())
// Output
invalid file "./demo.txt", please make sure the file exists
Todo List
  • Encryption and decryption by aes
  • Encryption and decryption by des
  • Encryption and decryption by 3aes
  • Encryption and decryption by rsa
  • Encryption and decryption by rc2
  • Encryption and decryption by rc4
  • Encryption and decryption by rc5
  • Encryption and decryption by rc6
  • Encryption and decryption by sm2
  • Encryption and decryption by sm3
  • Encryption and decryption by sm4

Documentation

Overview

Package dongle is a simple, semantic and developer-friendly golang package for encoding&decoding and encryption&decryption.

Index

Constants

View Source
const (
	CBC = "cbc"
	ECB = "ecb"
	CFB = "cfb"
	OFB = "ofb"
	CTR = "ctr"
	GCM = "gcm"
)

group mode constant

View Source
const (
	NoPadding       = "no_padding"
	ZeroPadding     = "zero_padding"
	PKCS1Padding    = "pkcs1_padding"
	PKCS5Padding    = "pkcs5_padding"
	PKCS7Padding    = "pkcs7_padding"
	TBCPadding      = "tbc_padding"
	AnsiX923Padding = "ansi_x923_padding"
	ISO10126Padding = "iso10226_padding"
)

padding mode constant

View Source
const (
	// BASE32 base32 encoding mode
	BASE32 = "base32"
	// BASE58 base58 encoding mode
	BASE58 = "base58"
	// BASE64 base64 encoding mode
	BASE64 = "base64"
	// HEX hex encoding mode
	HEX = "hex"
)

Variables

View Source
var (

	// Encode returns a new encode instance
	Encode = newEncode()
	// Decode returns a new decode instance
	Decode = newDecode()
	// Encrypt returns a new encrypt instance
	Encrypt = newEncrypt()
	// Decrypt returns a new decrypt instance
	Decrypt = newDecrypt()
)

Functions

This section is empty.

Types

type Cipher added in v0.0.2

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

Cipher defines a Cipher struct.

func NewCipher added in v0.0.2

func NewCipher() *Cipher

NewCipher returns a new Cipher instance.

func (*Cipher) CBCDecrypt added in v0.0.2

func (c *Cipher) CBCDecrypt(block cipher.Block, src []byte) []byte

CBCDecrypt returns a byte slice which decrypts with cbc mode.

func (*Cipher) CBCEncrypt added in v0.0.2

func (c *Cipher) CBCEncrypt(block cipher.Block, src []byte) []byte

CBCEncrypt returns a byte slice which encrypts with cbc mode.

func (*Cipher) PKCS5Padding added in v0.0.2

func (c *Cipher) PKCS5Padding(src []byte) []byte

PKCS5Padding padding with pkcs5 mode.

func (*Cipher) PKCS5UnPadding added in v0.0.2

func (c *Cipher) PKCS5UnPadding(src []byte) []byte

PKCS5UnPadding unpadding with pkcs5 mode.

func (*Cipher) PKCS7Padding added in v0.0.2

func (c *Cipher) PKCS7Padding(src []byte, blockSize int) []byte

PKCS7Padding padding with pkcs7 mode.

func (*Cipher) PKCS7UnPadding added in v0.0.2

func (c *Cipher) PKCS7UnPadding(src []byte) []byte

PKCS7UnPadding unpadding with pkcs7 mode.

func (*Cipher) SetIV added in v0.0.2

func (c *Cipher) SetIV(iv interface{})

SetIV sets initial vector.

func (*Cipher) SetKey added in v0.0.2

func (c *Cipher) SetKey(key interface{})

SetSecretKey sets secret key.

func (*Cipher) SetMode added in v0.0.2

func (c *Cipher) SetMode(mode string)

SetMode sets group mode.

func (*Cipher) SetPadding added in v0.0.2

func (c *Cipher) SetPadding(padding string)

SetPadding sets padding mode.

func (*Cipher) ZeroPadding added in v0.0.2

func (c *Cipher) ZeroPadding(src []byte, blockSize int) []byte

ZeroPadding padding with zero mode.

func (*Cipher) ZeroUnPadding added in v0.0.2

func (c *Cipher) ZeroUnPadding(src []byte) []byte

ZeroUnPadding unpadding with zero mode.

Jump to

Keyboard shortcuts

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