private static EncryptionInstructions EncryptEnvelopeKeyUsingSymmetricKeyV2()

in src/EncryptionUtilsV2.cs [187:216]


        private static EncryptionInstructions EncryptEnvelopeKeyUsingSymmetricKeyV2(EncryptionMaterialsV2 materials)
        {
            var aes = materials.SymmetricProvider as Aes;
            if (aes == null)
            {
                throw new NotSupportedException("AES is the only supported algorithm with this method.");
            }

            switch (materials.SymmetricProviderType)
            {
                case SymmetricAlgorithmType.AesGcm:
                {
                    var aesObject = Aes.Create();
                    var nonce = aesObject.IV.Take(DefaultNonceSize).ToArray();
                    var associatedText = Encoding.UTF8.GetBytes(XAmzAesGcmCekAlgValue);
                    var cipher = AesGcmUtils.CreateCipher(true, materials.SymmetricProvider.Key, DefaultTagBitsLength, nonce, associatedText);
                    var envelopeKey = cipher.DoFinal(aesObject.Key);

                    var encryptedEnvelopeKey = nonce.Concat(envelopeKey).ToArray();

                    var instructions = new EncryptionInstructions(materials.MaterialsDescription, aesObject.Key, encryptedEnvelopeKey, nonce,
                        XAmzWrapAlgAesGcmValue, XAmzAesGcmCekAlgValue);
                    return instructions;
                }
                default:
                {
                    throw new NotSupportedException($"{materials.SymmetricProviderType} isn't supported with SymmetricProvider");
                }
            }
        }