首页 > 后端开发 > Golang > 正文

Implementasi Metode 标准对称加密签名 pada Golang

PHPz
发布: 2024-07-18 20:37:52
原创
556 人浏览过

Image description

什么是标准对称加密签名方法?

所以,你看,这种方法是一种加密数据的方法,这样数据就安全了,没有解密密钥的人就无法读取数据。想象一下,朋友们,你有一本用挂锁锁着的日记。只有拥有钥匙的人才能打开并阅读你的日记。

对称加密

对称加密就像朋友和朋友,这是什么,哈哈哈,重点是,就像拥有同一把钥匙开锁一样。该密钥用于加密(锁定)和解密(解锁)数据。所以,只要你有钥匙,好友和好友都可以锁定和解锁相同的数据。

签名

这里的签名不是物理签名,更多的是数字签名。这个签名保证了发送的数据是真正来自朋友的,并且没有人中途更改过数据。所以朋友们,您可以确定您收到的数据是来自源头的真实数据,没有被篡改。

为什么要使用这个方法?

  • 数据安全:您肯定希望您的数据免遭恶意之手,对吗?通过对称加密,您的数据被加密,只有拥有密钥的人才能打开。
  • 数据完整性:通过签名,您可以确保您接收或发送的数据是真实的且未被篡改。所以朋友们,你们不用担心有人作弊。
  • 效率:对称加密通常比非对称加密更快,因为加密和解密过程更简单。

Golang 中的使用示例

现在让我们看看如何在 Golang 中使用该方法。

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)
}

登录后复制

签名 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)
}

登录后复制

因此标准对称加密签名方法对于维护数据的安全性和完整性非常重要。通过对称加密,您可以对数据进行加密以使其安全,通过签名,您可以确保您接收或发送的数据是真实的且未被篡改。所以,请确保朋友们在各种需要高安全性的需求时使用此方法。

来源:

  • HMAC Go
  • SHA256

以上是Implementasi Metode 标准对称加密签名 pada Golang的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!