How get SHA1 RSA signed of bytes in Go?

huangapple go评论87阅读模式
英文:

How get SHA1 RSA signed of bytes in Go?

问题

我是你的中文翻译助手,以下是你要翻译的内容:

我是Go语言的新手,很难弄清楚crypto包的工作原理。

基本上,我需要使用pem密钥对字符串进行签名,并获取其SHA1值,就像下面C#中的等效代码一样。

谢谢

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); 
rsa.FromXmlString("<RSAKeyValue><Modulus>oQRshGhLf2Fh...</Modulus>"); 
byte[] signMain = rsa.SignData(Encoding.UTF8.GetBytes(data), new SHA1CryptoServiceProvider());
英文:

I'm new in Go and having hard time to figure it out how crypto package works.

Basicly I need to sign a string with pem key and get sha1 of that like the equivalent code in C# in below.

Thanks

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); 
rsa.FromXmlString(“&lt;RSAKeyValue&gt;&lt;Modulus&gt;oQRshGhLf2Fh...”);
byte[] signMain = rsa.SignData(Encoding.UTF8.GetBytes(data), new SHA1CryptoServiceProvider());

答案1

得分: 6

C#代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace RSASHA1
{
    class Program
    {
        static void Main(string[] args)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.FromXmlString("<RSAKeyValue><Modulus>n9t+R7Zmn4VLaWmixLnUkxfQmwDHqRmtlC3mR0YJ9ntUpqLPIkbZgGbuCM8jzmt3kTs4UIufzxQfim1v8avBYDkijxKRN4/g2juS9IqmWjvdV4CrxfTVGXudiuzeqI3wzoaHY4BmMdhmOA46GV6pifAVeQnJb6Hp6/F9bwi1kFE=</Modulus><Exponent>AQAB</Exponent><P>+ow3plgxHR5BcBApdHu93bLQJHD+DbcUV6qKSHMfQNosjDb6IiPUd6kfHI34DIldot6ooI+AUqZYIqW57J7llw==</P><Q>o1YL/4UkELk5nmU30NA7gLmnZm+bynt9rR2yjkU55SP60tqO5/72aXak/RrSih1BC6WfJxEr2OLMWud6t8wGVw==</Q><DP>U8vXxPPUIVgALNnK86F7RA3NHZMI9U9ZJ2TrcQXH2yndIlw01nxDUG4o8KTu5EqBHbr4jRLRqVLnQVdKUsBmKw==</DP><DQ>XeJJxzeaQVqWOetoJ8hpS1ZrWD/yxnIxDN6zEX/NBV6m/6fM2KD+tiQSNcHDHswt5Snvzx7ZmzLRz7aaHSdSxQ==</DQ><InverseQ>j/DPSjRS09ephgGgiSBFcpxDc+54n6Fo2M2zLhMj/lBoO8klbNPB4YlwFHJjEh9EpbLB1Zwi7XjL2weFX2Ws+g==</InverseQ><D>HRtdMfePVgpyQb5fSczAXQwgCqkosaygQ3PcEyw4ki3AqxrH9mjKH6weOXJmBabhvYr35QqXTaBPYegtUubALda87T7cviK1rCPN18vv+eU4MT9TADQyj9HeYvp7fUfjEfaIaf/QpAXwSRrF/n9CsM1ef4aZX4XFd2uv2SH7yVU=</D></RSAKeyValue>");
            byte[] signMain = rsa.SignData(Encoding.UTF8.GetBytes("hello"), new SHA1CryptoServiceProvider());
            StringBuilder hex = new StringBuilder(signMain.Length * 2);
            foreach (byte b in signMain) hex.AppendFormat("{0:x2}", b);
            Console.Write(hex.ToString());
            Console.ReadLine();
        }
    }
}

Go代码(用于在OpenSSL < 1.0和OpenSSL >= 1.0中生成的密钥)

package main

import (
	"encoding/pem"
	"fmt"
	"crypto/sha1"
	"crypto/rsa"
	"crypto/rand"
	"crypto/x509"
	"crypto"
	"encoding/hex"
)

var openssl_10_key string = `-----BEGIN PRIVATE KEY-----
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMipwvuAvELnymqg2cdNTX+Df341aT6iZC/5qSv+U2a6tr/sqoIRd4Hi5TQFB0yPcbqL5NhIz8CjrA7k7WROH6F10OKfhPgyxBxRARLSqfVdflFb9WF3BlJeOgHti8j24a38gUsqx42nkbLJM6tmlYqSOSZWR3jPx7ZuqgSNO+G5AgMBAAECgYAGXKN+HjTzZhS1ae4dphmSTmGVzcaAohAzf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sE/rrwK71D7mvAkEAzsPAPCuOKlLjS51FIW06StDDVzcQNuaDsBx0wqhUC8Wq2Rio+yqgWIk+G0fU3rL3189iKb7zDn+FFrOe2gS9FwJBAK0xJxRpM39JYXjGemqebaQl/J5vnSWDyydoslMsRxn2h/AL8U6zbPnMqc76K+/sRv+uqfkY50Xtb8Ak5FIlkjUCQQDKjYWi7GBHIhA9IQJ6LpDuffj/VZfy0HutaRMRJum+5pPqPXg9JZMTaM+I1iO5TsXxit31W3SsfUwZ4zj5B69dAkAVttlUIc4O+S7K6jXolRjA10bZT7nkiAn8WMVwog1XNEi4vUaQhqPH7IgflJPRDZy5Ov71tfx/SKrUu60Gzcuu
-----END RSA PRIVATE KEY-----`

func main() {
	h := sha1.New()
	h.Write([]byte("hello"))
	sum := h.Sum(nil)

	// For RSA keys generated with OpenSSL < 1.0
	pem09, _ := pem.Decode([]byte(openssl_09_key))
	privateKey09, _ := x509.ParsePKCS1PrivateKey(pem09.Bytes)
	sig, _ := rsa.SignPKCS1v15(rand.Reader, privateKey09, crypto.SHA1, sum)
	fmt.Println("OpenSSL < 1.0", hex.EncodeToString(sig))

	// For RSA keys generated with OpenSSL >= 1.0
	pem10, _ := pem.Decode([]byte(openssl_10_key))
	privateKey10I, _ := x509.ParsePKCS8PrivateKey(pem10.Bytes)
	privateKey10 := privateKey10I.(*rsa.PrivateKey)
	sig, _ = rsa.SignPKCS1v15(rand.Reader, privateKey10, crypto.SHA1, sum)
	fmt.Println("OpenSSL >= 1.0", hex.EncodeToString(sig))
}

以上是给定的C#和Go代码的翻译。如果你有任何其他问题,请随时问我。

英文:

C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace RSASHA1
{
class Program
{
static void Main(string[] args)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(&quot;&lt;RSAKeyValue&gt;&lt;Modulus&gt;n9t+R7Zmn4VLaWmixLnUkxfQmwDHqRmtlC3mR0YJ9ntUpqLPIkbZgGbuCM8jzmt3kTs4UIufzxQfim1v8avBYDkijxKRN4/g2juS9IqmWjvdV4CrxfTVGXudiuzeqI3wzoaHY4BmMdhmOA46GV6pifAVeQnJb6Hp6/F9bwi1kFE=&lt;/Modulus&gt;&lt;Exponent&gt;AQAB&lt;/Exponent&gt;&lt;P&gt;+ow3plgxHR5BcBApdHu93bLQJHD+DbcUV6qKSHMfQNosjDb6IiPUd6kfHI34DIldot6ooI+AUqZYIqW57J7llw==&lt;/P&gt;&lt;Q&gt;o1YL/4UkELk5nmU30NA7gLmnZm+bynt9rR2yjkU55SP60tqO5/72aXak/RrSih1BC6WfJxEr2OLMWud6t8wGVw==&lt;/Q&gt;&lt;DP&gt;U8vXxPPUIVgALNnK86F7RA3NHZMI9U9ZJ2TrcQXH2yndIlw01nxDUG4o8KTu5EqBHbr4jRLRqVLnQVdKUsBmKw==&lt;/DP&gt;&lt;DQ&gt;XeJJxzeaQVqWOetoJ8hpS1ZrWD/yxnIxDN6zEX/NBV6m/6fM2KD+tiQSNcHDHswt5Snvzx7ZmzLRz7aaHSdSxQ==&lt;/DQ&gt;&lt;InverseQ&gt;j/DPSjRS09ephgGgiSBFcpxDc+54n6Fo2M2zLhMj/lBoO8klbNPB4YlwFHJjEh9EpbLB1Zwi7XjL2weFX2Ws+g==&lt;/InverseQ&gt;&lt;D&gt;HRtdMfePVgpyQb5fSczAXQwgCqkosaygQ3PcEyw4ki3AqxrH9mjKH6weOXJmBabhvYr35QqXTaBPYegtUubALda87T7cviK1rCPN18vv+eU4MT9TADQyj9HeYvp7fUfjEfaIaf/QpAXwSRrF/n9CsM1ef4aZX4XFd2uv2SH7yVU=&lt;/D&gt;&lt;/RSAKeyValue&gt;&quot;);
byte[] signMain = rsa.SignData(Encoding.UTF8.GetBytes(&quot;hello&quot;), new SHA1CryptoServiceProvider());
StringBuilder hex = new StringBuilder(signMain.Length * 2);
foreach (byte b in signMain) hex.AppendFormat(&quot;{0:x2}&quot;, b);
Console.Write(hex.ToString());
Console.ReadLine();
}
}
}

Go code for keys generated in OpenSSL < 1.0 and OpenSSL >= 1.0

package main
import (
&quot;encoding/pem&quot;
&quot;fmt&quot;
&quot;crypto/sha1&quot;
&quot;crypto/rsa&quot;
&quot;crypto/rand&quot;
&quot;crypto/x509&quot;
&quot;crypto&quot;
&quot;encoding/hex&quot;
)
var openssl_10_key string = `-----BEGIN PRIVATE KEY-----
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMipwvuAvELnymqg2cdNTX+Df341aT6iZC/5qSv+U2a6tr/sqoIRd4Hi5TQFB0yPcbqL5NhIz8CjrA7k7WROH6F10OKfhPgyxBxRARLSqfVdflFb9WF3BlJeOgHti8j24a38gUsqx42nkbLJM6tmlYqSOSZWR3jPx7ZuqgSNO+G5AgMBAAECgYAGXKN+HjTzZhS1ae4dphmSTmGVzcaAohAzf5XZrp5UTN0lNP3e69DYwSnR8WR4cx/NWBWxHPXa1+Jiz01B3dw3NgdcK9YhheDJEzBjMV8kizqTV2GrwGrc04JI/77uBZdj8bUx3pIhwWOK6ShmhveuPE8Q3EfeZ5g+yKvkF4YTNQJBAPhyENG0qp36VWLyHC4tY7j35tTjYuhTNDsmPI/BNYaSIGzGGfKZweAouUsetk3BQe4fgDHdU+wT+uvArvUPua8CQQDOw8A8K44qUuNLnUUhbTpK0MNXNxA25oOwHHTCqFQLxarZGKj7KqBYiT4bR9TesvfXz2IpvvMOf4UWs57aBL0XAkEArTEnFGkzf0lheMZ6ap5tpCX8nm+dJYPLJ2iyUyxHGfaH8AvxTrNs+cypzvor7+xG/66p+RjnRe1vwCTkUiWSNQJBAMqNhaLsYEciED0hAnoukO59+P9Vl/LQe61pExEm6b7mk+o9eD0lkxNoz4jWI7lOxfGK3fVbdKx9TBnjOPkHr10CQBW22VQhzg75LsrqNeiVGMDXRtlPueSICfxYxXCiDVc0SLi9RpCGo8fsiB+Uk9ENnLk6/vW1/H9IqtS7rQbNy64=
-----END PRIVATE KEY-----`
var openssl_09_key string = `-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDIqcL7gLxC58pqoNnHTU1/g39+NWk+omQv+akr/lNmura/7KqCEXeB4uU0BQdMj3G6i+TYSM/Ao6wO5O1kTh+hddDin4T4MsQcUQES0qn1XX5RW/VhdwZSXjoB7YvI9uGt/IFLKseNp5GyyTOrZpWKkjkmVkd4z8e2bqoEjTvhuQIDAQABAoGABlyjfh4082YUtWnuHaYZkk5hlc3GgKIQM3+V2a6eVEzdJTT93uvQ2MEp0fFkeHMfzVgVsRz12tfiYs9NQd3cNzYHXCvWIYXgyRMwYzFfJIs6k1dhq8Bq3NOCSP++7gWXY/G1Md6SIcFjiukoZob3rjxPENxH3meYPsir5BeGEzUCQQD4chDRtKqd+lVi8hwuLWO49+bU42LoUzQ7JjyPwTWGkiBsxhnymcHgKLlLHrZNwUHuH4Ax3VPsE/rrwK71D7mvAkEAzsPAPCuOKlLjS51FIW06StDDVzcQNuaDsBx0wqhUC8Wq2Rio+yqgWIk+G0fU3rL3189iKb7zDn+FFrOe2gS9FwJBAK0xJxRpM39JYXjGemqebaQl/J5vnSWDyydoslMsRxn2h/AL8U6zbPnMqc76K+/sRv+uqfkY50Xtb8Ak5FIlkjUCQQDKjYWi7GBHIhA9IQJ6LpDuffj/VZfy0HutaRMRJum+5pPqPXg9JZMTaM+I1iO5TsXxit31W3SsfUwZ4zj5B69dAkAVttlUIc4O+S7K6jXolRjA10bZT7nkiAn8WMVwog1XNEi4vUaQhqPH7IgflJPRDZy5Ov71tfx/SKrUu60Gzcuu
-----END RSA PRIVATE KEY-----`
func main() {
h := sha1.New()
h.Write([]byte(&quot;hello&quot;))
sum := h.Sum(nil)
// For RSA keys generated with OpenSSL &lt; 1.0
pem09, _ := pem.Decode([]byte(openssl_09_key))
privateKey09, _ := x509.ParsePKCS1PrivateKey(pem09.Bytes)
sig, _ := rsa.SignPKCS1v15(rand.Reader, privateKey09, crypto.SHA1, sum)
fmt.Println(&quot;OpenSSL &lt; 1.0&quot;, hex.EncodeToString(sig))
// For RSA keys generated with OpenSSL &gt;= 1.0
pem10, _ := pem.Decode([]byte(openssl_10_key))
privateKey10I, _ := x509.ParsePKCS8PrivateKey(pem10.Bytes)
privateKey10 := privateKey10I.(*rsa.PrivateKey)
sig, _ = rsa.SignPKCS1v15(rand.Reader, privateKey10, crypto.SHA1, sum)
fmt.Println(&quot;OpenSSL &gt;= 1.0&quot;, hex.EncodeToString(sig))
}

huangapple
  • 本文由 发表于 2016年2月22日 01:04:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/35539083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定