Skip to content

Instantly share code, notes, and snippets.

@zhehaowang
Created December 19, 2016 23:48
Show Gist options
  • Save zhehaowang/c49b8278773dc7c7bc487b4cae8710b9 to your computer and use it in GitHub Desktop.
Save zhehaowang/c49b8278773dc7c7bc487b4cae8710b9 to your computer and use it in GitHub Desktop.
Unity test for sha256oid
using UnityEngine;
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
public class TestRegister : MonoBehaviour {
public string test() {
CspParameters cspParams = new CspParameters();
cspParams.KeyContainerName = "ThisIsAKey"; // This is the key used to encrypt and decrypt can be anything.
var provider = new RSACryptoServiceProvider(cspParams);
// Check if _RegString exsists, if not create it with an encrypted value of -1
byte[] tempencryptedBytes = provider.Encrypt(System.Text.Encoding.UTF8.GetBytes ("hello"), true);
// Get the value stored in RegString and decrypt it using the key.
string decrypted = System.Text.Encoding.UTF8.GetString(provider.Decrypt(tempencryptedBytes, true));
string id = CryptoConfig.MapNameToOID("SHA256");
// "2.16.840.1.101.3.4.2.1": this causes a complaint
var signature = provider.SignData(tempencryptedBytes, id);
string tempencryptionString = Convert.ToBase64String (signature);
Console.Out.WriteLine(tempencryptionString);
// this is fine in Unity. In console both are fine and output the same result
var signature1 = provider.SignData(tempencryptedBytes, "SHA256");
string tempencryptionString1 = Convert.ToBase64String (signature1);
Console.Out.WriteLine(tempencryptionString1);
return id;
}
void Start() {
test();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment