How to sign a hash in Swift with CryptoKit

290 views Asked by At

I'm trying to port this code from Node.js to Swift using CryptoKit.

const sign = crypto.createSign('sha256');
sign.write(hashString);
sign.end();

const signature = sign.sign({ 
    key: privateKey, // read from a .pem file
    passphrase: "passphrase",
}); 

const signature64 = signature.toString("base64");

My private key is stored in a .pem file.

I can create the hash like this in Swift with CryptoKit, but I'm not sure how to sign it with my private key.

let digest = SHA256.hash(data: hashData)

EDIT: This is how I'm generating the key pair.

openssl genrsa -des3 -out WM_IO_my_rsa_key_pair 2048 

I was also trying with CommonCrypto since this doesn't seem possible with CryptoKit.

let string = "..."
let hash = UnsafeMutablePointer<UInt8>.allocate(capacity: Int(CC_SHA256_DIGEST_LENGTH))
defer { hash.deallocate() }
CC_SHA256(string, CC_LONG(string.count), hash)
0

There are 0 answers