Как получить SHA1 RSA со знаком байт в Go?
Я новичок в Go и с трудом понимаю, как работает крипто-пакет.
В основном мне нужно подписать строку с ключом pem и получить sha1 из этого, как эквивалентный код в C# ниже.
Спасибо
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(“<RSAKeyValue><Modulus>oQRshGhLf2Fh...”);
byte[] signMain = rsa.SignData(Encoding.UTF8.GetBytes(data), new SHA1CryptoServiceProvider());
1 ответ:
Код 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
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+HjTzZhS1ae4dphmSTmGVzcaAohAzf5XZrp5UTN0lNP3e69DYwSnR8WR4cx/NWBWxHPXa1+Jiz01B3dw3NgdcK9YhheDJEzBjMV8kizqTV2GrwGrc04JI/77uBZdj8bUx3pIhwWOK6ShmhveuPE8Q3EfeZ5g+yKvkF4YTNQJBAPhyENG0qp36VWLyHC4tY7j35tTjYuhTNDsmPI/BNYaSIGzGGfKZweAouUsetk3BQe4fgDHdU+wT+uvArvUPua8CQQDOw8A8K44qUuNLnUUhbTpK0MNXNxA25oOwHHTCqFQLxarZGKj7KqBYiT4bR9TesvfXz2IpvvMOf4UWs57aBL0XAkEArTEnFGkzf0lheMZ6ap5tpCX8nm+dJYPLJ2iyUyxHGfaH8Avx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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)) }