"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Implementasi Metode Standard Symmetric Encryption Signature pada Golang

Implementasi Metode Standard Symmetric Encryption Signature pada Golang

Published on 2024-07-31
Browse:389

Image description

What is the Standard Symmetric Encryption Signature Method?

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

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.

Signature

The signature here is not a physical signature, but rather 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.

Why should you use this method?

  • Data Security: Surely you want your data to be safe from malicious hands, right? With symmetric encryption, your data is encrypted and can only be opened by the person who has the key.
  • Data Integrity: With a signature, you can ensure that the data you receive or send is genuine and has not been tampered with. So, friends, you don't need to worry about someone cheating.
  • Efficiency: Symmetric encryption is usually faster than Asymmetric Encryption because the encryption and decryption process is simpler.

Example of Usage in Golang

Now let's see how to use this method in Golang.

Symmetric Encryption 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) 



Signature Golang

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:

  • HMAC Go
  • SHA256
Release Statement This article is reproduced at: https://dev.to/yogameleniawan/implementasi-metode-standard-symmetric-encryption-signature-pada-golang-2m5m?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3