Amazon.S3.Encryption.EncryptionUtils.UpdateMetadataWithEncryptionInstructions C# (CSharp) Method

UpdateMetadataWithEncryptionInstructions() static private method

Update the request's ObjectMetadata with the necessary information for decrypting the object.
static private UpdateMetadataWithEncryptionInstructions ( AmazonWebServiceRequest request, EncryptionInstructions instructions ) : void
request Amazon.Runtime.AmazonWebServiceRequest /// AmazonWebServiceRequest encrypted using the given instruction ///
instructions EncryptionInstructions /// Non-null instruction used to encrypt the data in this AmazonWebServiceRequest . ///
return void
        internal static void UpdateMetadataWithEncryptionInstructions(AmazonWebServiceRequest request, EncryptionInstructions instructions)
        {
            byte[] keyBytesToStoreInMetadata = instructions.EncryptedEnvelopeKey;
            string base64EncodedEnvelopeKey = Convert.ToBase64String(keyBytesToStoreInMetadata);

            byte[] IVToStoreInMetadata = instructions.InitializationVector;
            string base64EncodedIV = Convert.ToBase64String(IVToStoreInMetadata);

            var putObjectRequest = request as PutObjectRequest;
            if (putObjectRequest != null)
            {
                MetadataCollection metadata = putObjectRequest.Metadata;
                metadata.Add(keyInMetadata, base64EncodedEnvelopeKey);
                metadata.Add(initVectorInMetadata, base64EncodedIV);

                Dictionary<string, string> materialsDescription = instructions.MaterialsDescription;
                if (materialsDescription.Count == 0)
                    metadata.Add(encryptionMaterialsDescription, "{}");

                putObjectRequest.Metadata = metadata;
            }

            var initiateMultipartrequest = request as InitiateMultipartUploadRequest;
            if (initiateMultipartrequest != null)
            {
                MetadataCollection metadata = initiateMultipartrequest.Metadata;
                metadata.Add(keyInMetadata, base64EncodedEnvelopeKey);
                metadata.Add(initVectorInMetadata, base64EncodedIV);

                Dictionary<string, string> materialsDescription = instructions.MaterialsDescription;
                if (materialsDescription.Count == 0)
                    metadata.Add(encryptionMaterialsDescription, "{}");

                initiateMultipartrequest.Metadata = metadata;
            }
        }

Usage Example

        /// <summary>
        /// Updates the request where the metadata contains encryption information
        /// and the input stream contains the encrypted object contents.
        /// </summary>
        /// <param name="putObjectRequest">
        /// The request whose contents are to be encrypted.
        /// </param>
        private void GenerateEncryptedObjectRequestUsingMetadata(PutObjectRequest putObjectRequest)
        {
            // Create instruction
            EncryptionInstructions instructions = EncryptionUtils.GenerateInstructions(this.encryptionMaterials);

            EncryptionUtils.AddUnencryptedContentLengthToMetadata(putObjectRequest);

            // Encrypt the object data with the instruction
            putObjectRequest.InputStream = EncryptionUtils.EncryptRequestUsingInstruction(putObjectRequest.InputStream, instructions);

            // Update the metadata
            EncryptionUtils.UpdateMetadataWithEncryptionInstructions(putObjectRequest, instructions);
        }
All Usage Examples Of Amazon.S3.Encryption.EncryptionUtils::UpdateMetadataWithEncryptionInstructions