golang SHA1WithRSA
In Go (Golang), you can use the crypto/rsa and crypto/sha1 packages to perform SHA1 with RSA encryption. Here's an example of how you can generate a SHA1 hash and sign it with an RSA private key:
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/x509"
"encoding/pem"
"fmt"
)
func main() {
// Generate RSA private key
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
fmt.Println("Failed to generate RSA private key:", err)
return
}
// Generate SHA1 hash
message := []byte("Hello, World!")
hash := sha1.Sum(message)
// Sign the hash with RSA private key
signature, err := rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.SHA1, hash[:])
if err != nil {
fmt.Println("Failed to sign the hash:", err)
return
}
// Encode the signature to PEM format
signaturePem := pem.EncodeToMemory(
&pem.Block{
Type: "RSA SIGNATURE",
Bytes: signature,
},
)
fmt.Println("Signature:", string(signaturePem))
}
In this example, we generate an RSA private key using rsa.GenerateKey function. Then, we calculate the SHA1 hash of a message using sha1.Sum function. Finally, we sign the hash with the RSA private key using rsa.SignPKCS1v15 function and encode the signature to PEM format using pem.EncodeToMemory function.
Note that this example uses SHA1, which is considered weak and should not be used for new applications. It is recommended to use stronger hash functions like SHA256 or SHA512 for better security
原文地址: https://www.cveoy.top/t/topic/iYPm 著作权归作者所有。请勿转载和采集!