news 2026/3/7 6:00:10

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/3/1 11:39:53

Open-AutoGLM vs 竞品:谁才是长链路AI任务处理的真正王者?

第一章:Open-AutoGLM 长链路任务处理竞品比拼在长链路自动化任务处理领域,Open-AutoGLM 凭借其模块化架构与动态推理能力,展现出显著优势。面对如 AutoGPT、LangChain 和 BabyAGI 等主流框架的竞争,Open-AutoGLM 在任务分解精度、…

作者头像 李华
网站建设 2026/3/3 19:25:23

Web安全测试

🍅 点击文末小卡片 ,免费获取软件测试全套资料,资料在手,涨薪更快 随着互联网时代的蓬勃发展,基于Web环境下的应用系统、应用软件也得到了越来越广泛的使用。目前,很多企业的业务发展都依赖于互联网&…

作者头像 李华
网站建设 2026/3/6 7:08:37

【干货收藏】企业AI架构实战指南:从框架搭建到场景落地的底层逻辑

企业AI架构可分为五层:硬件层、数据层、模型层、AI应用插件层和应用层。部署方式需根据业务需求选择云端、本地或混合部署。架构规划应遵循三个原则:对齐业务、数据底座扎实、部署方式匹配需求。企业AI落地的关键是"用对的架构解决对的问题"&a…

作者头像 李华
网站建设 2026/3/5 8:47:36

13、FPGA更新与可编程性:安全与应用解析

FPGA更新与可编程性:安全与应用解析 1. 引言 与专用集成电路(ASIC)不同,静态随机存取存储器(SRAM)现场可编程门阵列(FPGA)在制造后能够改变其逻辑配置。定义该逻辑的比特流存储在非易失性片外存储器中,并在FPGA上电时加载到FPGA上。这种特性十分有用,若在逻辑设计中…

作者头像 李华