/// <summary> /// Verify signature. /// </summary> /// <param name="content">content to be signed.</param> /// <param name="sign">the signature to verified.</param> /// <param name="publicKey">public key.</param> /// <returns>if verifying signature succeed.</returns> public static bool DoCheck(sbyte[] content, string sign, string publicKey) { try { KeyFactory keyFactory = KeyFactory.GetInstance("RSA"); sbyte[] encodedKey = (sbyte[])(Array)Base64.Decode(publicKey, Base64Flags.Default); IPublicKey pubKey = keyFactory.GeneratePublic(new X509EncodedKeySpec((byte[])(Array)encodedKey)); Java.Security.Signature signature = Java.Security.Signature.GetInstance(SignAlgorithms256); signature.InitVerify(pubKey); signature.Update((byte[])(Array)content); return(signature.Verify(Base64.Decode(sign, Base64Flags.Default))); } catch (Exception ex) { Log.Info("RSA", "DoCheck fail: " + ex.Message); throw; } }
/// <summary> /// Sign content. /// </summary> /// <param name="content">data to be signed.</param> /// <param name="privateKey">merchant's private key.</param> /// <returns> Signed value.</returns> public static string SignContent(string content, string privateKey) { try { PKCS8EncodedKeySpec privatePKCS8 = new PKCS8EncodedKeySpec(Base64.Decode(privateKey, Base64Flags.Default)); KeyFactory keyf = KeyFactory.GetInstance("RSA"); IPrivateKey priKey = keyf.GeneratePrivate(privatePKCS8); Java.Security.Signature signatureObj = Java.Security.Signature.GetInstance(SignAlgorithms256); signatureObj.InitSign(priKey); signatureObj.Update(Encoding.UTF8.GetBytes(content)); sbyte[] signed = (sbyte[])(Array)signatureObj.Sign(); return(Base64.EncodeToString((byte[])(Array)signed, Base64Flags.Default)); } catch (Exception ex) { Log.Info("RSA", "Sign fail: " + ex.Message); throw; } }
private void ShowBiometricPrompt(Java.Security.Signature signature) { // Create biometric prompt var activity = MainActivity.FormsContext; var negativeButtonListener = new DialogInterfaceOnClickListener(() => { // Do something here. }); biometricPrompt = new BiometricPrompt.Builder(activity) .SetDescription("Never Been Easier") .SetTitle("Biometric Prompt Authentication") .SetSubtitle("Please allow Xamarin Life to authenticate") .SetNegativeButton("Cancel", activity.MainExecutor, negativeButtonListener) .Build(); // Show biometric prompt var cancellationSignal = new CancellationSignal(); var authenticationCallback = GetAuthenticationCallback(); biometricPrompt.Authenticate(new BiometricPrompt.CryptoObject(signature), cancellationSignal, activity.MainExecutor, authenticationCallback); }