我是Golang的新手,想要实现我的旧版C-Program的功能。
我有一个" DSA私钥"使用密码和" CERTIFICATE"
在C中我做了:
{
char *privkey=
"-----BEGIN DSA PRIVATE KEY-----\n"
"Proc-Type: 4,ENCRYPTED\n"
"DEK-Info: DES-EDE3-CBC, xxxxxx\n"
"-----END DSA PRIVATE KEY-----\n"
"-----BEGIN CERTIFICATE-----\n"
"xxxx\n"
"Og==\n"
"-----END CERTIFICATE-----\n"
;
ERR_load_crypto_strings();
OpenSSL_add_all_algorithms();
BIO *mem = BIO_new(BIO_s_mem());
BIO_puts(mem, privkey);
dsa = PEM_read_bio_DSAPrivateKey(mem, NULL, NULL, "passphrase");
Byte hash[EVP_MAX_MD_SIZE];
unsigned int hashLen = EVP_MAX_MD_SIZE;
EVP_MD_CTX mdctx;
EVP_MD_CTX_init(&mdctx);
EVP_DigestInit_ex(&mdctx, EVP_dss1(), NULL);
EVP_DigestUpdate(&mdctx, data, dataLen);
EVP_DigestFinal_ex(&mdctx, hash, &hashLen);
EVP_MD_CTX_cleanup(&mdctx);
Byte signatur[1024];
memset(signatur, 0, 1024);
char buf[200];
short fehler = 0;
signaturLen = DSA_size(dsa);
fehler = !DSA_sign(0, hash, hashLen, signatur, &signaturLen, dsa);
if (fehler) {
ps("FEHLER: signieren gescheitert");
ps(ERR_error_string(ERR_get_error(), buf));
}
// do something with signatur
DSA_free(dsa);
}
它按预期工作。我如何用golang实现这一目标?我试过了
func (s SignConfig) ReadPrivateKey() (*rsa.PrivateKey, error) {
b, e := ioutil.ReadFile(filepath.Join(core.ConfigDir(), s.PrivateKey))
if e != nil {
return nil, e
}
block, _ := pem.Decode(b)
b, e = x509.DecryptPEMBlock(block, []byte(s.PassPhrase))
priv, e := x509.ParsePKCS1PrivateKey(b)
if e != nil {
return nil, e
}
return priv, nil
}
阅读私钥,但我得到了asn1.StructuralError{Msg:"integer too large"}