news 2026/6/3 23:42:58

go SHA256WithRSA

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
go SHA256WithRSA

In Go,SHA256WithRSArefers to RSA digital signatures with SHA-256 hashing, a common standard for signing and verification. Here's how to implement it:

1. Generate RSA Key Pair

go

复制

package main import ( "crypto/rand" "crypto/rsa" "crypto/x509" "encoding/pem" "fmt" "os" ) func generateKeys() (*rsa.PrivateKey, error) { // Generate 2048-bit RSA key pair privateKey, err := rsa.GenerateKey(rand.Reader, 2048) if err != nil { return nil, err } return privateKey, nil } // Save keys to PEM files func savePrivateKey(key *rsa.PrivateKey, filename string) error { file, err := os.Create(filename) if err != nil { return err } defer file.Close() privateKeyBytes := x509.MarshalPKCS1PrivateKey(key) privateKeyPEM := &pem.Block{ Type: "RSA PRIVATE KEY", Bytes: privateKeyBytes, } return pem.Encode(file, privateKeyPEM) }

2. Sign Data with SHA256 + RSA

go

复制

import ( "crypto" "crypto/rand" "crypto/rsa" "crypto/sha256" ) func signData(privateKey *rsa.PrivateKey, data []byte) ([]byte, error) { // Hash the data with SHA-256 hash := sha256.Sum256(data) // Sign the hash using RSA-PSS (recommended) or PKCS#1 v1.5 signature, err := rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.SHA256, hash[:]) if err != nil { return nil, err } return signature, nil }

3. Verify Signature

go

复制

func verifySignature(publicKey *rsa.PublicKey, data, signature []byte) error { hash := sha256.Sum256(data) // Verify the signature err := rsa.VerifyPKCS1v15(publicKey, crypto.SHA256, hash[:], signature) if err != nil { return fmt.Errorf("signature verification failed: %w", err) } return nil }

4. Complete Example

go

复制

package main import ( "crypto" "crypto/rand" "crypto/rsa" "crypto/sha256" "fmt" ) func main() { // Generate keys privateKey, err := rsa.GenerateKey(rand.Reader, 2048) if err != nil { panic(err) } // Data to sign data := []byte("Hello, SHA256WithRSA!") // Sign signature, err := signData(privateKey, data) if err != nil { panic(err) } fmt.Printf("Signature: %x\n", signature) // Verify publicKey := &privateKey.PublicKey err = verifySignature(publicKey, data, signature) if err != nil { panic(err) } fmt.Println("✅ Signature verified successfully!") }

Key Points

  • crypto.SHA256: Specifies SHA-256 as the hash algorithm

  • rsa.SignPKCS1v15: PKCS#1 v1.5 padding (widely compatible)

  • rsa.SignPSS: More secure PSS padding (recommended for new systems)

  • Hash first: Always hash data before signing; RSA operates on the hash digest

Thecrypto/x509andencoding/pempackages handle key serialization for storage and exchange.

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/28 22:14:26

自动化测试牢记这6点常见误区

🍅 点击文末小卡片 ,免费获取软件测试全套资料,资料在手,涨薪更快 我经常与一些客户打交道,他们的测试自动化要么刚刚开始,要么正在努力成长,而且他们常常都犯同样的致命错误。 虽然他们可能了…

作者头像 李华
网站建设 2026/6/2 21:18:24

性能测试流程详解

🍅 点击文末小卡片 ,免费获取软件测试全套资料,资料在手,涨薪更快性能测试概念我们经常看到的性能测试概念,有人或称之为性能策略,或称之为性能方法,或称之为性能场景分类,大概可以看…

作者头像 李华
网站建设 2026/6/3 1:26:12

Windows 11 安装 Visual Studio Code 完整指南

📝 适合人群:编程初学者、开发者、学生 ⏱️ 预计时间:5-10 分钟 🎯 学习目标:成功在 Windows 11 上安装并配置 Visual Studio Code 📖 什么是 Visual Studio Code? Visual Studio Code&#x…

作者头像 李华
网站建设 2026/6/3 5:01:59

FaceFusion人脸融合在在线教育教师形象定制中的应用

FaceFusion人脸融合在在线教育教师形象定制中的应用 在今天的在线课堂里,一位数学老师正用清晰的逻辑讲解微积分,但镜头前略显疲惫的面容和单调的表情却让学生的注意力逐渐涣散。另一边,另一位教师虽内容相同,但画面中呈现出的是经…

作者头像 李华
网站建设 2026/6/3 17:13:11

大模型显存计算:手把手教你计算Llama 70B所需显存

文章详细介绍了大模型(以Llama 70B为例)推理所需GPU显存的计算方法,包括模型权重显存(140GB)、KV Cache显存(800GB)和其他开销(94GB),总计约1TB。文章强调KV Cache是显存占用的主要部分,受并发用户数和上下文长度影响显…

作者头像 李华