private static ITINKAead InitAead(string keyName)
        {
            TINKKeysetHandle handle = LoadOrCreateKey(keyName, CreateAeadKey);
            ITINKAead        aead   = TINKAeadFactory.PrimitiveWithKeysetHandle(handle, out NSError error);

            if (error != null)
            {
                throw new Exception("Failed to create Aead: " + error.LocalizedFailureReason);
            }

            return(aead);
        }
예제 #2
0
        private void TestTink()
        {
            RegisterAead();

            // gen key
            NSError error;
            var     tpl = new TINKAeadKeyTemplate(TINKAeadKeyTemplates.Aes256Gcm, out error);

            if (error != null)
            {
                Console.WriteLine("Error: " + error);
                return;
            }
            var handle = new TINKKeysetHandle(tpl, out error);

            if (error != null)
            {
                Console.WriteLine("Error: " + error);
                return;
            }

            // store
            var keysetName = "co.tnn.tink.demo_key";

            if (!handle.WriteToKeychainWithName(keysetName, true, out error))
            {
                Console.WriteLine("Error: " + error);
                return;
            }

            // load
            TINKKeysetHandle handleStore = new TINKKeysetHandle(keysetName, out error);

            if (error != null)
            {
                Console.WriteLine("Error: " + error);
                return;
            }

            // AEAD
            ITINKAead aead = TINKAeadFactory.PrimitiveWithKeysetHandle(handleStore, out error);

            if (error != null)
            {
                Console.WriteLine("Error: " + error);
                return;
            }

            // encrypt
            NSData cipher = aead.Encrypt(NSData.FromString("hello world", NSStringEncoding.UTF8), NSData.FromString("k_value", NSStringEncoding.UTF8), out error);

            if (error != null)
            {
                Console.WriteLine("Error: " + error);
                return;
            }

            Console.WriteLine(cipher.GetBase64EncodedString(NSDataBase64EncodingOptions.None));

            // decrypt
            var plain = aead.Decrypt(cipher, NSData.FromString("k_value", NSStringEncoding.UTF8), out error);

            if (error != null)
            {
                Console.WriteLine("Error: " + error);
                return;
            }
            Console.WriteLine(plain.ToString(NSStringEncoding.UTF8));
        }