Home > Article > Backend Development > Implementasi Metode Standard Symmetric Encryption Signature pada Golang
So, you see, this method is a way to encrypt data so that it is safe and cannot be read by people who don't have the decryption key. Just imagine, friends, you have a diary that you lock with a padlock. Only people who have the key can open and read your diaries.
Symmetric encryption is like friends and friends, what's this, hahaha, the point is, it's like having the same key to open the lock. This key is used for encryption (locking) and decryption (unlocking) data. So, both friends and friends can lock and unlock the same data as long as you have the key.
The signature here is not a physical signature, but more of a digital signature. This signature ensures that the data sent is truly from friends and no one has changed the data midway. So, friends, you can be sure that the data you receive is genuine from the source and has not been tampered with.
Now let's see how to use this method in Golang.
package main import ( "crypto/aes" "crypto/cipher" "crypto/rand" "encoding/hex" "fmt" "io" ) func encrypt(key, text []byte) (string, error) { block, err := aes.NewCipher(key) if err != nil { return "", err } ciphertext := make([]byte, aes.BlockSize+len(text)) iv := ciphertext[:aes.BlockSize] if _, err := io.ReadFull(rand.Reader, iv); err != nil { return "", err } stream := cipher.NewCFBEncrypter(block, iv) stream.XORKeyStream(ciphertext[aes.BlockSize:], text) return fmt.Sprintf("%x", ciphertext), nil } func decrypt(key []byte, cryptoText string) (string, error) { ciphertext, _ := hex.DecodeString(cryptoText) block, err := aes.NewCipher(key) if err != nil { return "", err } if len(ciphertext) < aes.BlockSize { return "", fmt.Errorf("ciphertext too short") } iv := ciphertext[:aes.BlockSize] ciphertext = ciphertext[aes.BlockSize:] stream := cipher.NewCFBDecrypter(block, iv) stream.XORKeyStream(ciphertext, ciphertext) return string(ciphertext), nil } func main() { key := []byte("the-key-has-to-be-32-bytes-long!") plaintext := "hello, world!" ciphertext, err := encrypt(key, []byte(plaintext)) if err != nil { fmt.Println("Error encrypting:", err) return } fmt.Printf("Encrypted: %s\n", ciphertext) decryptedText, err := decrypt(key, ciphertext) if err != nil { fmt.Println("Error decrypting:", err) return } fmt.Printf("Decrypted: %s\n", decryptedText) }
package main import ( "crypto/hmac" "crypto/sha256" "encoding/hex" "fmt" ) func createHMAC(key, message []byte) string { mac := hmac.New(sha256.New, key) mac.Write(message) return hex.EncodeToString(mac.Sum(nil)) } func verifyHMAC(key, message []byte, signature string) bool { expectedMAC := createHMAC(key, message) return hmac.Equal([]byte(expectedMAC), []byte(signature)) } func main() { key := []byte("my-secret-key") message := []byte("important message") signature := createHMAC(key, message) fmt.Printf("Signature: %s\n", signature) isValid := verifyHMAC(key, message, signature) fmt.Printf("Is valid: %t\n", isValid) }
So the Standard Symmetric Encryption Signature Method is important for maintaining the security and integrity of your data. With symmetric encryption, you can encrypt your data to make it safe, and with a signature, you can ensure that the data you receive or send is genuine and has not been tampered with. So, make sure friends use this method for all kinds of needs that require high security.
Source:
The above is the detailed content of Implementasi Metode Standard Symmetric Encryption Signature pada Golang. For more information, please follow other related articles on the PHP Chinese website!