I’m trying to implement Google OAuth 2.0 in UnityScript (or Unity C#, whatever) and I’m stucked on creating a signature.
Accoding to Google documentation, I need to:
Sign the UTF-8 representation of the input using SHA256withRSA
(also known as RSASSA-PKCS1-V1_5-SIGN with the SHA-256 hash function)
with the private key
So, I have in input:
- some text need to be encrypted
- binary private key
- password for the private key
And I need to make the encryption to finally get the crypted sequence.
For now, I’ve found the following code:
var sha: SHA256 = new SHA256Managed();
sha.ComputeHash(text);
But this seems to be not what I’m looking for. The code above just crypts a text squence, not using my private key.
UPDATE
For now, I’ve wrote the following code:
var cert: X509Certificate2 =
new X509Certificate2(google_pvt_key.ToArray(), "notasecret");
var rsa: RSACryptoServiceProvider = cert.PrivateKey;
var rsaf: RSAPKCS1SignatureFormatter = new RSAPKCS1SignatureFormatter(rsa);
rsaf.SetHashAlgorithm("SHA256");
var sgn: String = Convert.ToBase64String(rsaf.CreateSignature(hash));
But I’m getting the following exception:
CryptographicException: bad hash length for System.Security.Cryptography.SHA256Managed
Mono.Security.Cryptography.PKCS1.Encode_v15 (System.Security.Cryptography.HashAlgorithm hash, System.Byte hashValue, Int32 emLength)
Mono.Security.Cryptography.PKCS1.Sign_v15 (System.Security.Cryptography.RSA rsa, System.Security.Cryptography.HashAlgorithm hash, System.Byte hashValue)
System.Security.Cryptography.RSAPKCS1SignatureFormatter.CreateSignature (System.Byte rgbHash)