There is a decryption and signature interface. I want to move from PHP to Golang. The PHP function is as follows:
function getSignature($param){
if (is_string($param)) {
$file_private = 'file.p12';
if (!$cert_store = file_get_contents($file_private)) {
return "Error: Unable to read the cert file
";
}
$signature = "";
$algo = "sha256WithRSAEncryption";
$password = "PASSWORD";
$private_key_file = openssl_pkcs12_read($cert_store, $cert_info, $password);
if ($private_key_file)
{
$private_key = $cert_info['pkey'];
openssl_sign($param, $signature, $private_key, $algo);
return htmlentities(base64_encode($signature));
}
}
return false;
}
I want to use golang to achieve. How can I convert into golang?
SOLVED
This is what actually my code in golang:
func Sign(privateKey *rsa.PrivateKey, data string) (string, error) {
h := crypto.SHA256.New()
h.Write([]byte(data))
hashed := h.Sum(nil)
sign, err := rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.SHA256, hashed)
if err != nil {
return "", err
}
return base64.RawURLEncoding.EncodeToString(sign), err
}
func read_keys() {
b, err := ioutil.ReadFile("file.p12")
if err != nil {
fmt.Println(err)
}
password := "PASSWORD"
privk, _, err := pkcs12.Decode(b, password)
if err != nil {
fmt.Println(err)
}
pv := privk.(*rsa.PrivateKey)
sign, _ := Sign(pv, "Your String Data")
fmt.Print(sign)
}