GoLang encryption method
The encryption results of the following Golang code are consistent with the results of Java language, so you need to pay attention to the result case problem.
package tool import ( "appback/src/logger" "bytes" "crypto/aes" "crypto/cipher" "crypto/hmac" "crypto/md5" "crypto/rand" "crypto/rsa" "crypto/sha1" "crypto/sha256" "crypto/sha512" "crypto/x509" "encoding/base64" "encoding/hex" "encoding/pem" "fmt" "strings" ) // md5 verificationfunc MD5Str(src string) string { h := () ([]byte(src)) // The string that needs to be encrypted is // ("%s\n", ((nil))) // Output encryption result return ((nil)) } // hmacsha256 verificationfunc HMAC_SHA256(src, key string) string { m := (, []byte(key)) ([]byte(src)) return ((nil)) } // hmacsha512 verificationfunc HMAC_SHA512(src, key string) string { m := (, []byte(key)) ([]byte(src)) return ((nil)) } func HMAC_SHA1(src, key string) string { m := (, []byte(key)) ([]byte(src)) return ((nil)) } // sha256 verificationfunc SHA256Str(src string) string { h := () ([]byte(src)) // The string that needs to be encrypted is // ("%s\n", ((nil))) // Output encryption result return ((nil)) } // sha512 verificationfunc SHA512Str(src string) string { h := () ([]byte(src)) // The string that needs to be encrypted is // ("%s\n", ((nil))) // Output encryption result return ((nil)) } // Base encodingfunc BASE64EncodeStr(src string) string { return string(([]byte(src))) } // base decodingfunc BASE64DecodeStr(src string) string { a, err := (src) if err != nil { return "" } return string(a) } var ivspec = []byte("0000000000000000") func AESEncodeStr(src, key string) string { block, err := ([]byte(key)) if err != nil { ("key error1", err) } if src == "" { ("plain content empty") } ecb := (block, ivspec) content := []byte(src) content = PKCS5Padding(content, ()) crypted := make([]byte, len(content)) (crypted, content) return (crypted) } func AESDecodeStr(crypt, key string) string { crypted, err := ((crypt)) if err != nil || len(crypted) == 0 { ("plain content empty") } block, err := ([]byte(key)) if err != nil { ("key error1", err) } ecb := (block, ivspec) decrypted := make([]byte, len(crypted)) (decrypted, crypted) return string(PKCS5Trimming(decrypted)) } func PKCS5Padding(ciphertext []byte, blockSize int) []byte { padding := blockSize - len(ciphertext)%blockSize padtext := ([]byte{byte(padding)}, padding) return append(ciphertext, padtext...) } func PKCS5Trimming(encrypt []byte) []byte { padding := encrypt[len(encrypt)-1] return encrypt[:len(encrypt)-int(padding)] } func RsaEncrypt(src, key string) string { block, _ := ([]byte(key)) if block == nil { return "" } pubInterface, err := () if err != nil { (()) return "" } pub := pubInterface.(*) crypted, err := rsa.EncryptPKCS1v15(, pub, []byte(src)) if err != nil { (()) return "" } return (crypted) }
Call
package main import ( "./tool" "fmt" ) func main() { (tool.MD5Str("111")) }
GoLang three-class encryption algorithm
Hash algorithm
name | Speed/Safety |
---|---|
crc32 | Fast speed, low safety 2^32 |
adler | Fast speed, low safety 2^32 |
crc64 | Slightly fast, low safety 2^64 |
md5 | Normal speed, average safety 2^128 |
sha1 | Normal speed, average safety 2^128 |
sha256 | Slow speed and high safety 2^256 |
sha512 | Slow speed, extremely safe 2^512 |
hash function application:
Message authentication is a mechanism or service used to verify the integrity of the message. Message authentication confirms that the received data is indeed the same as when it was sent (i.e., tamper-proof), and it also ensures that the identity of the sender is true and valid (i.e., tamper-proof).
In other words, the hash function only determines that the information comes from the producer, and only has a verification function and cannot be used for information transmission because there is no decryption algorithm.
Golang implementation of algorithms in tables
import package:
import ( "hash/crc32" "hash/crc64" "hash/adler32" "crypto/sha512" "crypto/sha256" "crypto/sha1" "crypto/md5" "encoding/hex" )
The teacher said that the hash function used for verification is generally not used alone. When defining the encryption interface, define a []string name of the hash function used to store the combination, such as: []string{"md5", "crc64", "sha256". "sha256"·····}
type AllHash struct { Alog []string }
The binding method hashs the data according to the hash name. These functions are perfunctory by the Go standard library and are explained within ten words, otherwise there will be nothing.
For md5, sha1, sha256, sha512 steps,
I guess:
(Take md5 as an example)
One object is equivalent to applying for a buf: myhash:=()
2. Write byte type data to this buf: ([]byte(laststr))
3. Perform the corresponding hash operation: bs:=(nil), I use reflection to see that the type of bs is []uint8.
4. The final data is output in hexadecimal: laststr=(bs) or ("%x", bs)
func (allhash *AllHash)GetBytesHash(data[]byte)string{ var laststr string laststr=string(data) for i:=0;i<len();i++{ switch [i] { case "md5": myhash:=() ([]byte(laststr)) bs:=(nil) laststr=(bs) case "sha1": myhash:=() ([]byte(laststr)) bs:=(nil) laststr=(bs) case "sha256": myhash:=() ([]byte(laststr)) bs:=(nil) laststr=(bs) case "sha512": myhash:=() ([]byte(laststr)) bs:=(nil) laststr=(bs) case "crc32": mycrc:=() (mycrc,laststr) laststr=("%x",mycrc.Sum32()) case "crc64": const ISO = 0xD800000000000000 tabISO := MakeTable(ISO) c := (tabISO) (c, laststr) s := c.Sum64() laststr=("%x",s) case "adler32": c := () (c, laststr) state, err := c.().MarshalBinary() if err!=nil{ (err) } laststr=(state) } }
Symmetric encryption
For symmetric encryption, the sender must first have a key, and then execute the encryption algorithm to obtain the encrypted data; the receiver must obtain the sender's key in advance and decrypt it with the key.
Symmetric encryption is suitable for encrypting a large amount of data. Since the transmission key is not secure, when it is actually used, the data is symmetrically encrypted and the key is asymmetrically encrypted.
DES encryption steps:
- 1. Determine the number of key bits. If not enough, make up for zeros, and the cutoff will be exceeded. Here, assume that the key is 24 bits
- 2. Call TripleDesEncrypt of the third-party library goEncrypt, and use the key to encrypt
- 3. Return encrypted data.
func Encrypt(datastr []byte,password []byte)[]byte { length:=len(password) if length<24{ for i:=0;i<=24-1-length;i++{ password=append(password,0) } }else if length>24 { password=password[:24] } cryptText, err := (datastr, password) if err != nil { (err) return []byte{} } return cryptText }
DES decryption steps:
- 1. Determine the number of key bits. If not enough, make up for zeros, and the cutoff will be exceeded. Here, assume that the key is 24 bits
- 2. Call TripleDecrypt of the third-party library goEncrypt and use the key to decrypt
- 3. Return decrypted data
func Decrypt(datastr []byte,password []byte)[]byte { length:=len(password) if length<24{ for i:=0;i<=24-1-length;i++{ password=append(password,0) } }else if length>24 { password=password[:24] } //(len(password)) newplaintext, err:= (datastr, password) if err != nil { (err) return []byte{} } return newplaintext }
Asymmetric encryption
Private key encryption, public key decryption; public key encryption, private key decryption, is called asymmetric encryption.
When both parties pass information, both parties need to create public and private keys. If they use private keys to encrypt, they must pass the public key to the other party, and the other party uses the public key to decrypt it.
Here is the ECCgolang implementation:
func (e *ECC )Encrypt(datastr []byte)[]byte { cryptText, err:= (datastr , []byte()) if err!=nil{ return []byte{} } return cryptText } func (e *ECC )Decrypt(datastr []byte)[]byte { msg, err := (datastr , []byte()) if err != nil { return []byte{} } return msg }
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.