Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   /*
    * Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
    *
    * Licensed under the Apache License, Version 2.0 (the "License").
    * You may not use this file except in compliance with the License.
    * A copy of the License is located at
    *
    *  http://aws.amazon.com/apache2.0
    *
   * or in the "license" file accompanying this file. This file is distributed
   * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
   * express or implied. See the License for the specific language governing
   * permissions and limitations under the License.
   */
  package com.amazonaws.services.s3.internal.crypto;
  
  import static com.amazonaws.util.LengthCheckInputStream.EXCLUDE_SKIPPED_BYTES;
  
  import java.security.Key;
  import java.util.HashMap;
  import java.util.Map;
  
  
This internal class is no longer used, and will be removed in the future. The EncryptionUtils class encrypts and decrypts data stored in S3. It can be used to prepare requests for encryption before they are stored in S3 and to decrypt objects that are retrieved from S3.
  
  public class EncryptionUtils {
    
Suffix appended to the end of instruction file names
  
      @Deprecated
      static final String INSTRUCTION_SUFFIX = ".instruction";

    
Returns an updated request where the metadata contains encryption information and the input stream contains the encrypted object contents. The specified encryption materials will be used to encrypt and decrypt data.

Deprecated:
no longer used and will be removed in the future
Parameters:
request The request whose contents are to be encrypted.
materials The encryption materials to be used to encrypt and decrypt data.
cryptoProvider The crypto provider whose encryption implementation will be used to encrypt data
Returns:
The updated request where the metadata is set up for encryption and input stream contains the encrypted contents.
  
      @Deprecated
      public static PutObjectRequest encryptRequestUsingMetadata(PutObjectRequest requestEncryptionMaterials materialsProvider cryptoProvider) {
          // Create instruction
          EncryptionInstruction instruction = EncryptionUtils.generateInstruction(materialscryptoProvider);
  
          // Encrypt the object data with the instruction
          PutObjectRequest encryptedObjectRequest = EncryptionUtils.encryptRequestUsingInstruction(requestinstruction);
  
          // Update the metadata
          EncryptionUtils.updateMetadataWithEncryptionInstructionrequestinstruction );
 
         return encryptedObjectRequest;
     }

    
Returns an updated object where the object content input stream contains the decrypted contents.

Deprecated:
no longer used and will be removed in the future
Parameters:
object The object whose contents are to be decrypted.
materials The encryption materials to be used to encrypt and decrypt data.
cryptoProvider The crypto provider whose encryption implementation will be used to decrypt data
Returns:
The updated object where the object content input stream contains the decrypted contents.
 
     @Deprecated
     public static S3Object decryptObjectUsingMetadata(S3Object objectEncryptionMaterials materialsProvider cryptoProvider) {
         // Create an instruction object from the object headers
         EncryptionInstruction instruction = EncryptionUtils.buildInstructionFromObjectMetadataobjectmaterialscryptoProvider );
 
         // Decrypt the object file with the instruction
         return EncryptionUtils.decryptObjectUsingInstruction(objectinstruction);
     }

    
Generates an instruction that will be used to encrypt an object.

Deprecated:
no longer used and will be removed in the future
Parameters:
materials The encryption materials to be used to encrypt and decrypt data.
cryptoProvider The crypto provider whose encryption implementation will be used to encrypt and decrypt data.
Returns:
The instruction that will be used to encrypt an object.
 
     @Deprecated
     public static EncryptionInstruction generateInstruction(EncryptionMaterials materialsProvider cryptoProvider) {
       return generateInstruction(new StaticEncryptionMaterialsProvider(materials), cryptoProvider);
     }

    

Deprecated:
no longer used and will be removed in the future
 
     @Deprecated
     public static EncryptionInstruction generateInstruction(EncryptionMaterialsProvider materialsProvider,
             Provider cryptoProvider) {
         return buildInstruction(materialsProvider.getEncryptionMaterials(), cryptoProvider);
     }

    

Deprecated:
no longer used and will be removed in the future
 
     @Deprecated
     public static EncryptionInstruction generateInstruction(EncryptionMaterialsProvider materialsProvider,
             Map<StringStringmaterialsDescriptionProvider cryptoProvider) {
         return buildInstruction(materialsProvider.getEncryptionMaterials(materialsDescription), cryptoProvider);
     }

    

Deprecated:
no longer used and will be removed in the future
 
     @Deprecated
     public static EncryptionInstruction buildInstruction(EncryptionMaterials materialsProvider cryptoProvider) {
         // Generate a one-time use symmetric key and initialize a cipher to
         // encrypt object data
         SecretKey envelopeSymmetricKey = generateOneTimeUseSymmetricKey();
         CipherFactory cipherFactory = new CipherFactory(envelopeSymmetricKey.nullcryptoProvider);
 
         // Encrypt the envelope symmetric key
         byte[] encryptedEnvelopeSymmetricKey = getEncryptedSymmetricKey(envelopeSymmetricKeymaterialscryptoProvider);
 
         // Return a new instruction with the appropriate fields.
         return new EncryptionInstruction(materials.getMaterialsDescription(), encryptedEnvelopeSymmetricKey,
                 envelopeSymmetricKeycipherFactory);
     }

    
Builds an instruction object from the contents of an instruction file.

Deprecated:
no longer used and will be removed in the future
Parameters:
instructionFile A non-null instruction file retrieved from S3 that contains encryption information
materials The non-null encryption materials to be used to encrypt and decrypt data.
cryptoProvider The crypto provider whose encryption implementation will be used to encrypt and decrypt data. Null is ok and uses the preferred provider from Security.getProviders().
Returns:
A non-null instruction object containing encryption information
 
     @Deprecated
     public static EncryptionInstruction buildInstructionFromInstructionFile(S3Object instructionFileEncryptionMaterials materialsProvider cryptoProvider) {
         return buildInstructionFromInstructionFile(instructionFilenew StaticEncryptionMaterialsProvider(materials), cryptoProvider);
     }

    
Builds an instruction object from the contents of an instruction file.

Deprecated:
no longer used and will be removed in the future
Parameters:
instructionFile A non-null instruction file retrieved from S3 that contains encryption information
materialsProvider The non-null encryption materials provider to be used to encrypt and decrypt data.
cryptoProvider The crypto provider whose encryption implementation will be used to encrypt and decrypt data. Null is ok and uses the preferred provider from Security.getProviders().
Returns:
A non-null instruction object containing encryption information
 
     @Deprecated
     public static EncryptionInstruction buildInstructionFromInstructionFile(S3Object instructionFileEncryptionMaterialsProvider materialsProviderProvider cryptoProvider) {
         JSONObject instructionJSON = parseJSONInstruction(instructionFile);
         try {
             // Get fields from instruction object
             String encryptedSymmetricKeyB64 = instructionJSON.getString(.);
             String ivB64 = instructionJSON.getString(.);
             String materialsDescriptionString = instructionJSON.tryGetString(.);
             Map<StringStringmaterialsDescription = convertJSONToMap(materialsDescriptionString);
 
             // Decode from Base 64 to standard binary bytes
             byte[] encryptedSymmetricKey = Base64.decode(encryptedSymmetricKeyB64);
             byte[] iv = Base64.decode(ivB64);
 
             if (encryptedSymmetricKey == null || iv == null) {
                 // If necessary encryption info was not found in the instruction file, throw an exception.
                 throw new AmazonClientException(
                         String.format("Necessary encryption info not found in the instruction file '%s' in bucket '%s'",
                                       instructionFile.getKey(), instructionFile.getBucketName()));
             }
 
             EncryptionMaterials materials = retrieveOriginalMaterials(materialsDescriptionmaterialsProvider);
             // If we're unable to retrieve the original encryption materials, we can't decrypt the object, so
             // throw an exception.
             if (materials == null) {
                 throw new AmazonClientException(
                         String.format("Unable to retrieve the encryption materials that originally " +
                                 "encrypted object corresponding to instruction file '%s' in bucket '%s'.",
                                 instructionFile.getKey(), instructionFile.getBucketName()));
             }
 
             // Decrypt the symmetric key and create the symmetric cipher
             SecretKey symmetricKey = getDecryptedSymmetricKey(encryptedSymmetricKeymaterialscryptoProvider);
             CipherFactory cipherFactory = new CipherFactory(symmetricKey.ivcryptoProvider);
 
             return new EncryptionInstruction(materialsDescriptionencryptedSymmetricKeysymmetricKeycipherFactory);
         } catch (JSONException e) {
             throw new AmazonClientException("Unable to parse retrieved instruction file : " + e.getMessage());
         }
     }

    
Builds an instruction object from the object metadata.

Deprecated:
no longer used and will be removed in the future
Parameters:
object A non-null object that contains encryption information in its headers
materials The non-null encryption materials to be used to encrypt and decrypt data.
cryptoProvider The crypto provider whose encryption implementation will be used to encrypt and decrypt data. Null is ok and uses the preferred provider from Security.getProviders().
Returns:
A non-null instruction object containing encryption information
Throws:
com.amazonaws.AmazonClientException if encryption information is missing in the metadata, or the encryption materials used to encrypt the object are not available via the materials Accessor
 
     @Deprecated
     public static EncryptionInstruction buildInstructionFromObjectMetadata(S3Object objectEncryptionMaterials materialsProvider cryptoProvider) {
       return buildInstructionFromObjectMetadata(objectnew StaticEncryptionMaterialsProvider(materials), cryptoProvider);
     }

    
Builds an instruction object from the object metadata.

Deprecated:
no longer used and will be removed in the future
Parameters:
object A non-null object that contains encryption information in its headers
materialsProvider The non-null encryption materials provider to be used to encrypt and decrypt data.
cryptoProvider The crypto provider whose encryption implementation will be used to encrypt and decrypt data. Null is ok and uses the preferred provider from Security.getProviders().
Returns:
A non-null instruction object containing encryption information
Throws:
com.amazonaws.AmazonClientException if encryption information is missing in the metadata, or the encryption materials used to encrypt the object are not available via the materials Accessor
 
     @Deprecated
     public static EncryptionInstruction buildInstructionFromObjectMetadata(S3Object objectEncryptionMaterialsProvider materialsProviderProvider cryptoProvider) {
         ObjectMetadata metadata = object.getObjectMetadata();
 
         // Get encryption info from metadata.
         byte[] encryptedSymmetricKeyBytes = getCryptoBytesFromMetadata(.metadata);
         byte[] initVectorBytes = getCryptoBytesFromMetadata(.metadata);
         String materialsDescriptionString = getStringFromMetadata(.metadata);
         Map<StringStringmaterialsDescription = convertJSONToMap(materialsDescriptionString);
 
         if (encryptedSymmetricKeyBytes == null || initVectorBytes == null) {
             // If necessary encryption info was not found in the instruction file, throw an exception.
             throw new AmazonClientException(
                     String.format("Necessary encryption info not found in the headers of file '%s' in bucket '%s'",
                                   object.getKey(), object.getBucketName()));
         }
 
         EncryptionMaterials materials = retrieveOriginalMaterials(materialsDescriptionmaterialsProvider);
         // If we're unable to retrieve the original encryption materials, we can't decrypt the object, so
         // throw an exception.
         if (materials == null) {
             throw new AmazonClientException(
                     String.format("Unable to retrieve the encryption materials that originally " +
                             "encrypted file '%s' in bucket '%s'.",
                             object.getKey(), object.getBucketName()));
         }
 
         // Decrypt the symmetric key and create the symmetric cipher
         SecretKey symmetricKey = getDecryptedSymmetricKey(encryptedSymmetricKeyBytesmaterialscryptoProvider);
         CipherFactory cipherFactory = new CipherFactory(symmetricKey.initVectorBytescryptoProvider);
 
         return new EncryptionInstruction(materialsDescriptionencryptedSymmetricKeyBytessymmetricKeycipherFactory);
     }

    
Returns an updated request where the input stream contains the encrypted object contents. The specified instruction will be used to encrypt data.

Deprecated:
no longer used and will be removed in the future
Parameters:
request The request whose contents are to be encrypted.
instruction The instruction that will be used to encrypt the object data.
Returns:
The updated request where the input stream contains the encrypted contents.
 
     @Deprecated
     public static PutObjectRequest encryptRequestUsingInstruction(PutObjectRequest requestEncryptionInstruction instruction) {
         // Create a new metadata object if there is no metadata already.
         ObjectMetadata metadata = request.getMetadata();
         if (metadata == null) {
             metadata = new ObjectMetadata();
         }
 
         // Record the original Content MD5, if present, for the unencrypted data
         if (metadata.getContentMD5() != null) {
             metadata.addUserMetadata(.metadata.getContentMD5());
         }
         
         // Removes the original content MD5 if present from the meta data.
         metadata.setContentMD5(null);
         
         // Record the original, unencrypted content-length so it can be accessed later
         final long plaintextLength = getUnencryptedContentLength(requestmetadata);
         if (plaintextLength >= 0) {
             metadata.addUserMetadata(.,
                 Long.toString(plaintextLength));
         }
 
         // Put the calculated length of the encrypted contents in the metadata
         long cryptoContentLength = calculateCryptoContentLength(instruction.getSymmetricCipher(), requestmetadata);
         if (cryptoContentLength >= 0) {
             metadata.setContentLength(cryptoContentLength);
         }
 
         request.setMetadata(metadata);
 
         // Create encrypted input stream
         request.setInputStream(getEncryptedInputStream(requestinstruction.getCipherFactory(), plaintextLength));
 
         // Treat all encryption requests as input stream upload requests, not as file upload requests.
         request.setFile(null);
 
         return request;
     }

    
Returns an updated object where the object content input stream contains the decrypted contents.

Deprecated:
no longer used and will be removed in the future
Parameters:
object The object whose contents are to be decrypted.
instruction The instruction that will be used to decrypt the object data.
Returns:
The updated object where the object content input stream contains the decrypted contents.
 
     @Deprecated
     public static S3Object decryptObjectUsingInstruction(S3Object objectEncryptionInstruction instruction) {
         S3ObjectInputStream objectContent = object.getObjectContent();
 
         InputStream decryptedInputStream = new RepeatableCipherInputStream(objectContentinstruction.getCipherFactory());
         object.setObjectContent(new S3ObjectInputStream(decryptedInputStreamobjectContent.getHttpRequest()));
         return object;
     }

    
Creates a put request to store the specified instruction object in S3.

Deprecated:
no longer used and will be removed in the future
Parameters:
request The put request for the original object to be stored in S3.
instruction The instruction object to be stored in S3.
Returns:
A put request to store the specified instruction object in S3.
 
     @Deprecated
     public static PutObjectRequest createInstructionPutRequest(PutObjectRequest requestEncryptionInstruction instruction) {
         JSONObject instructionJSON = convertInstructionToJSONObject(instruction);
         byte[] instructionBytes = instructionJSON.toString().getBytes();
         InputStream instructionInputStream = new ByteArrayInputStream(instructionBytes);
 
         ObjectMetadata metadata = request.getMetadata();
 
         // Set the content-length of the upload
         metadata.setContentLength(instructionBytes.length);
 
         // Set the crypto instruction file header
         metadata.addUserMetadata(."");
 
         // Update the instruction request
         request.setKey(request.getKey() + );
         request.setMetadata(metadata);
         request.setInputStream(instructionInputStream);
 
         return request;
     }

    

Deprecated:
no longer used and will be removed in the future
 
     @Deprecated
     public static PutObjectRequest createInstructionPutRequest(String bucketNameString keyEncryptionInstruction instruction) {
         JSONObject instructionJSON = convertInstructionToJSONObject(instruction);
         byte[] instructionBytes = instructionJSON.toString().getBytes();
         InputStream instructionInputStream = new ByteArrayInputStream(instructionBytes);
 
         ObjectMetadata metadata = new ObjectMetadata();
         metadata.setContentLength(instructionBytes.length);
         metadata.addUserMetadata(."");
 
         return new PutObjectRequest(bucketNamekey + instructionInputStreammetadata);
     }

    
Creates a get object request for an instruction file using the default instruction file suffix.

Deprecated:
no longer used and will be removed in the future
Parameters:
id an S3 object id (not the instruction file id)
Returns:
A get request to retrieve an instruction file from S3.
 
     @Deprecated
         return createInstructionGetRequest(idnull);
     }

    
Creates and return a get object request for an instruction file.

Deprecated:
no longer used and will be removed in the future
Parameters:
s3objectId an S3 object id (not the instruction file id)
instFileSuffix suffix of the specific instruction file to be used, or null if the default instruction file is to be used.
 
     @Deprecated
             S3ObjectId s3objectIdString instFileSuffix) {
         return new GetObjectRequest(
                 s3objectId.instructionFileId(instFileSuffix));
     }

    
Creates a delete request to delete an instruction file in S3.

Deprecated:
no longer used and will be removed in the future
Parameters:
request The delete request for the original object to be deleted from S3.
Returns:
A delete request to delete an instruction file in S3.
 
     @Deprecated
         return new DeleteObjectRequest(request.getBucketName(), request.getKey() + );
     }

    
Returns true if the specified S3Object contains encryption info in its metadata, false otherwise.

Deprecated:
no longer used and will be removed in the future
Parameters:
retrievedObject An S3Object
Returns:
True if the specified S3Object contains encryption info in its metadata, false otherwise.
 
     @Deprecated
     public static boolean isEncryptionInfoInMetadata(S3Object retrievedObject) {
         Map<StringStringmetadata = retrievedObject.getObjectMetadata().getUserMetadata();
         return metadata != null
             && metadata.containsKey(.)
             && metadata.containsKey(.);
     }

    
Returns true if the specified S3Object is an instruction file containing encryption info, false otherwise.

Deprecated:
no longer used and will be removed in the future
Parameters:
instructionFile An S3Object that may potentially be an instruction file
Returns:
True if the specified S3Object is an instruction file containing encryption info, false otherwise.
 
     @Deprecated
     public static boolean isEncryptionInfoInInstructionFile(S3Object instructionFile) {
         if (instructionFile == null) {
             return false;
         }
         Map<StringStringmetadata = instructionFile.getObjectMetadata().getUserMetadata();
         if (metadata == null) {
             return false;
         }
         return metadata.containsKey(.);
     }

    
Adjusts a user specified range to retrieve all of the cipher blocks (each of size 16 bytes) that contain the specified range. For Chained Block Cipher decryption to function properly, we need to retrieve the cipher block that precedes the range, all of the cipher blocks that contain the range, and the cipher block that follows the range.

Deprecated:
no longer used and will be removed in the future
Parameters:
range A two-element array of longs corresponding to the start and finish (inclusive) of a desired range of bytes.
Returns:
A two-element array of longs corresponding to the start and finish of the cipher blocks to be retrieved. If the range is invalid, then return null.
 
     @Deprecated
     public static long[] getAdjustedCryptoRange(long[] range) {
         // If range is invalid, then return null.
         if (range == null || range[0] > range[1]) {
             return null;
         }
         long[] adjustedCryptoRange = new long[2];
         adjustedCryptoRange[0] = getCipherBlockLowerBound(range[0]);
         adjustedCryptoRange[1] = getCipherBlockUpperBound(range[1]);
         return adjustedCryptoRange;
     }

    
Adjusts the retrieved S3Object so that the object contents contain only the range of bytes desired by the user. Since encrypted contents can only be retrieved in CIPHER_BLOCK_SIZE (16 bytes) chunks, the S3Object potentially contains more bytes than desired, so this method adjusts the contents range.

Deprecated:
no longer used and will be removed in the future
Parameters:
object The S3Object retrieved from S3 that could possibly contain more bytes than desired by the user.
range A two-element array of longs corresponding to the start and finish (inclusive) of a desired range of bytes.
Returns:
The S3Object with adjusted object contents containing only the range desired by the user. If the range specified is invalid, then the S3Object is returned without any modifications.
 
     @Deprecated
     public static S3Object adjustOutputToDesiredRange(S3Object objectlong[] range) {
         if (range == null || range[0] > range[1]) {
             // Make no modifications if range is invalid.
             return object;
         } else {
             try {
                 S3ObjectInputStream objectContent = object.getObjectContent();
                 InputStream adjustedRangeContents = new AdjustedRangeInputStream(objectContentrange[0], range[1]);
                 object.setObjectContent(new S3ObjectInputStream(adjustedRangeContentsobjectContent.getHttpRequest()));
                 return object;
             } catch (IOException e) {
                 throw new AmazonClientException("Error adjusting output to desired byte range: " + e.getMessage());
             }
         }
     }

    
Generates a one-time use Symmetric Key on-the-fly for use in envelope encryption.
 
     public static SecretKey generateOneTimeUseSymmetricKey() {
         KeyGenerator generator;
         try {
             generator = KeyGenerator.getInstance(.);
             generator.init(.new SecureRandom());
             return generator.generateKey();
         } catch (NoSuchAlgorithmException e) {
             throw new AmazonClientException("Unable to generate envelope symmetric key:" + e.getMessage(), e);
         }
     }

    
Creates a symmetric cipher in the specified mode from the given symmetric key and IV. The given crypto provider will provide the encryption implementation. If the crypto provider is null, then the default JCE crypto provider will be used.

Deprecated:
no longer used and will be removed in the future
 
     @Deprecated
     public static Cipher createSymmetricCipher(SecretKey symmetricCryptoKeyint encryptModeProvider cryptoProviderbyte[] initVector) {
         try {
             Cipher cipher;
             if (cryptoProvider != null) {
                 cipher = Cipher.getInstance(.cryptoProvider);
             } else {
                 cipher = Cipher.getInstance(.);
             }
             if (initVector != null) {
                 cipher.init(encryptModesymmetricCryptoKeynew IvParameterSpec(initVector));
             } else {
                 cipher.init(encryptModesymmetricCryptoKey);
             }
             return cipher;
         } catch (Exception e) {
             throw new AmazonClientException("Unable to build cipher: " + e.getMessage() +
                     "\nMake sure you have the JCE unlimited strength policy files installed and " +
                     "configured for your JVM: http://www.ngs.ac.uk/tools/jcepolicyfiles"e);
         }
     }

    
Encrypts a symmetric key using the provided encryption materials and returns it in raw byte array form.

Deprecated:
no longer used and will be removed in the future
 
     @Deprecated
     public static byte[] getEncryptedSymmetricKey(SecretKey toBeEncryptedEncryptionMaterials materialsProvider cryptoProvider) {
         Key keyToDoEncryption;
         if (materials.getKeyPair() != null) {
             // Do envelope encryption with public key from key pair
             keyToDoEncryption = materials.getKeyPair().getPublic();
         } else {
             // Do envelope encryption with symmetric key
             keyToDoEncryptionmaterials.getSymmetricKey();
         }
         try {
             Cipher cipher;
             byte[] toBeEncryptedBytes = toBeEncrypted.getEncoded();
             if (cryptoProvider != null) {
                 cipher = Cipher.getInstance(keyToDoEncryption.getAlgorithm(), cryptoProvider);
             } else {
                 cipher = Cipher.getInstance(keyToDoEncryption.getAlgorithm()); // Use default JCE Provider
             }
             cipher.init(.keyToDoEncryption);
             return cipher.doFinal(toBeEncryptedBytes);
         } catch (Exception e) {
             throw new AmazonClientException("Unable to encrypt symmetric key: " + e.getMessage(), e);
         }
     }

    
Decrypts an encrypted symmetric key using the provided encryption materials and returns it as a SecretKey object.

Deprecated:
no longer used and will be removed in the future
 
     @Deprecated
     private static SecretKey getDecryptedSymmetricKey(byte[] encryptedSymmetricKeyBytesEncryptionMaterials materialsProvider cryptoProvider) {
         Key keyToDoDecryption;
         if (materials.getKeyPair() != null) {
             // Do envelope decryption with private key from key pair
             keyToDoDecryption = materials.getKeyPair().getPrivate();
         } else {
             // Do envelope decryption with symmetric key
             keyToDoDecryption = materials.getSymmetricKey();
         }
         try {
             Cipher cipher;
             if (cryptoProvider != null) {
                 cipher = Cipher.getInstance(keyToDoDecryption.getAlgorithm(), cryptoProvider);
             } else {
                 cipher = Cipher.getInstance(keyToDoDecryption.getAlgorithm());
             }
             cipher.init(.keyToDoDecryption);
             byte[] decryptedSymmetricKeyBytes = cipher.doFinal(encryptedSymmetricKeyBytes);
             return new SecretKeySpec(decryptedSymmetricKeyBytes.);
         } catch (Exception e) {
             throw new AmazonClientException("Unable to decrypt symmetric key from object metadata : " + e.getMessage(), e);
         }
     }

    

Deprecated:
no longer used and will be removed in the future
Parameters:
plaintextLength the expected total number of bytes of the plaintext; or -1 if not available.
 
     @Deprecated
     private static InputStream getEncryptedInputStream(
             PutObjectRequest requestCipherFactory cipherFactory,
             long plaintextLength) {
         try {
             InputStream is = request.getInputStream();
             if (request.getFile() != null) {
                 // Historically file takes precedence over the original input
                 // stream
                 is = new RepeatableFileInputStream(request.getFile());
             }
             if (plaintextLength > -1) {
                 // This ensures the plain-text read from the underlying data
                 // stream has the same length as the expected total
                 is = new LengthCheckInputStream(isplaintextLength,
                         );
             }
             return new RepeatableCipherInputStream(iscipherFactory);
         } catch (Exception e) {
             throw new AmazonClientException("Unable to create cipher input stream: " + e.getMessage(), e);
         }
     }

    

Deprecated:
no longer used and will be removed in the future
 
     @Deprecated
     public static ByteRangeCapturingInputStream getEncryptedInputStream(UploadPartRequest requestCipherFactory cipherFactory) {
         try {
             InputStream originalInputStream = request.getInputStream();
             if (request.getFile() != null) {
                 originalInputStream = new InputSubstream(new RepeatableFileInputStream(request.getFile()),
                         request.getFileOffset(), request.getPartSize(), request.isLastPart());
             }
 
             originalInputStream = new RepeatableCipherInputStream(originalInputStreamcipherFactory);
 
             if (request.isLastPart() == false) {
                 // We want to prevent the final padding from being sent on the stream...
                 originalInputStream = new InputSubstream(originalInputStream, 0, request.getPartSize(), false);
             }
 
             long partSize = request.getPartSize();
             int cipherBlockSize = cipherFactory.createCipher().getBlockSize();
             return new ByteRangeCapturingInputStream(originalInputStreampartSize - cipherBlockSizepartSize);
         } catch (Exception e) {
             throw new AmazonClientException("Unable to create cipher input stream: " + e.getMessage(), e);
         }
     }

    
Retrieves the byte[] value of either the crypto key or crypto IV. If these fields are not found in the metadata, returns null. Note: The bytes are transported in Base64-encoding, so they are decoded before they are returned.

Deprecated:
no longer used and will be removed in the future
 
     @Deprecated
     private static byte[] getCryptoBytesFromMetadata(String headerNameObjectMetadata metadatathrows NullPointerException {
         Map<StringStringuserMetadata = metadata.getUserMetadata();
         if (userMetadata == null || !userMetadata.containsKey(headerName)) {
             return null;
         } else {
             // Convert Base64 bytes to binary data.
             return Base64.decode(userMetadata.get(headerName));
         }
     }

    
Retrieves the String value of the given header from the metadata. Returns null if the field is not found in the metadata.

Deprecated:
no longer used and will be removed in the future
 
     @Deprecated
     private static String getStringFromMetadata(String headerNameObjectMetadata metadatathrows NullPointerException {
         Map<StringStringuserMetadata = metadata.getUserMetadata();
         if (userMetadata == null || !userMetadata.containsKey(headerName)) {
             return null;
         } else {
             return userMetadata.get(headerName);
         }
     }

    
Converts the JSON encoded materials description to a Map<String, String>

Deprecated:
no longer used and will be removed in the future
 
     @Deprecated
     @SuppressWarnings("unchecked"// Suppresses Iterator<String> type warning
     private static Map<StringStringconvertJSONToMap(String descriptionJSONString) {
         if (descriptionJSONString == null) {
             return null;
         }
         try {
             JSONObject descriptionJSON = new JSONObject(descriptionJSONString);
             Iterator<StringkeysIterator = descriptionJSON.keys();
             Map<StringStringmaterialsDescription = new HashMap<StringString>();
             while(keysIterator.hasNext()) {
                 String key = keysIterator.next();
                 materialsDescription.put(keydescriptionJSON.getString(key));
             }
             return materialsDescription;
         } catch (JSONException e) {
             throw new AmazonClientException("Unable to parse encryption materials description from metadata :" + e.getMessage());
         }
     }

    
Update the request's ObjectMetadata with the necessary information for decrypting the object

Deprecated:
no longer used and will be removed in the future
Parameters:
request Non-null PUT request encrypted using the given instruction
instruction Non-null instruction used to encrypt the data in this PUT request.
 
     @Deprecated
     public static void updateMetadataWithEncryptionInstruction(PutObjectRequest requestEncryptionInstruction instruction){
         byte[] keyBytesToStoreInMetadata = instruction.getEncryptedSymmetricKey();
         Cipher symmetricCipher = instruction.getSymmetricCipher();
         Map<StringStringmaterialsDescription = instruction.getMaterialsDescription();
 
         ObjectMetadata metadata = request.getMetadata();
         if (metadata == nullmetadata = new ObjectMetadata();
 
         if (request.getFile() != null) {
             Mimetypes mimetypes = Mimetypes.getInstance();
             metadata.setContentType(mimetypes.getMimetype(request.getFile()));
         }
 
         updateMetadata(metadatakeyBytesToStoreInMetadatasymmetricCiphermaterialsDescription);
         request.setMetadatametadata );
     }

    

Deprecated:
no longer used and will be removed in the future
 
     @Deprecated
     private static void updateMetadata(ObjectMetadata metadatabyte[] keyBytesToStoreInMetadataCipher symmetricCipherMap<StringStringmaterialsDescription) {
         // If we generated a symmetric key to encrypt the data, store it in the object metadata.
         if (keyBytesToStoreInMetadata != null) {
             metadata.addUserMetadata(.,
                     Base64.encodeAsString(keyBytesToStoreInMetadata));
         }
 
         // Put the cipher initialization vector (IV) into the object metadata
         metadata.addUserMetadata(.,
                 Base64.encodeAsString(symmetricCipher.getIV()));
 
         // Put the materials description into the object metadata as JSON
         JSONObject descriptionJSON = new JSONObject(materialsDescription);
         metadata.addUserMetadata(.descriptionJSON.toString());
     }

    

Deprecated:
no longer used and will be removed in the future
 
     @Deprecated
     public static ObjectMetadata updateMetadataWithEncryptionInfo(InitiateMultipartUploadRequest requestbyte[] keyBytesToStoreInMetadataCipher symmetricCipherMap<StringStringmaterialsDescription) {
         ObjectMetadata metadata = request.getObjectMetadata();
         if (metadata == nullmetadata = new ObjectMetadata();
 
         updateMetadata(metadatakeyBytesToStoreInMetadatasymmetricCiphermaterialsDescription);
 
         return metadata;
     }

    
Retrieve the original materials corresponding to the specified materials description. Returns null if unable to retrieve the original materials.

Deprecated:
no longer used and will be removed in the future
 
     @Deprecated
     private static EncryptionMaterials retrieveOriginalMaterials(Map<StringStringmaterialsDescriptionEncryptionMaterialsAccessor accessor) {
         if (accessor == null)
             return null;
         return accessor.getEncryptionMaterials(materialsDescription);
     }

    
Calculates the length of the encrypted file given the original plaintext file length and the cipher that will be used for encryption.

Deprecated:
no longer used and will be removed in the future
Returns:
The size of the encrypted file in bytes, or -1 if no content length has been set yet.
 
     @Deprecated
     private static long calculateCryptoContentLength(Cipher symmetricCipherPutObjectRequest requestObjectMetadata metadata) {
         long plaintextLength = getUnencryptedContentLength(requestmetadata);
 
         // If we don't know the unencrypted size, then report -1
         if (plaintextLength < 0) return -1;
 
         long cipherBlockSize = symmetricCipher.getBlockSize();
         long offset = cipherBlockSize - (plaintextLength % cipherBlockSize);
         return plaintextLength + offset;
     }

    

Deprecated:
no longer used and will be removed in the future
 
     @Deprecated
     public static long calculateCryptoContentLength(Cipher symmetricCipherUploadPartRequest request) {
         long plaintextLength;
         if (request.getFile() != null) {
             if (request.getPartSize() > 0) plaintextLength = request.getPartSize();
             else plaintextLength = request.getFile().length();
         } else if (request.getInputStream() != null) {
             plaintextLength = request.getPartSize();
         } else {
             return -1;
         }
         long cipherBlockSize = symmetricCipher.getBlockSize();
         long offset = cipherBlockSize - (plaintextLength % cipherBlockSize);
         return plaintextLength + offset;
     }

    
Returns the content length of the unencrypted data in a PutObjectRequest, or -1 if the original content-length isn't known.

Deprecated:
no longer used and will be removed in the future
Parameters:
request The request to examine.
metadata The metadata for the request.
Returns:
The content length of the unencrypted data in the request, or -1 if it isn't known.
 
     @Deprecated
     private static long getUnencryptedContentLength(PutObjectRequest requestObjectMetadata metadata) {
         if (request.getFile() != null) {
             return request.getFile().length();
         } else if (request.getInputStream() != null
                    && metadata.getRawMetadataValue(.) != null) {
             return metadata.getContentLength();
         }
 
         return -1;
     }

    
Returns a JSONObject representation of the instruction object.

Deprecated:
no longer used and will be removed in the future
 
     @Deprecated
     private static JSONObject convertInstructionToJSONObject(EncryptionInstruction instruction) {
         JSONObject instructionJSON = new JSONObject();
         try {
             JSONObject materialsDescriptionJSON = new JSONObject(
                     instruction.getMaterialsDescription());
             instructionJSON.put(.,
                     materialsDescriptionJSON.toString());
             instructionJSON.put(.
                 Base64.encodeAsString(instruction.getEncryptedSymmetricKey()));
             byte[] iv = instruction.getSymmetricCipher().getIV();
             instructionJSON.put(., Base64.encodeAsString(iv));
         } catch (JSONException e) {} // Keys are never null, so JSONException will never be thrown.
         return instructionJSON;
     }

    
Parses instruction data retrieved from S3 and returns a JSONObject representing the instruction
 
     @Deprecated
     private static JSONObject parseJSONInstruction(S3Object instructionObject) {
         try {
             String instructionString = convertStreamToString(instructionObject.getObjectContent());
             return new JSONObject(instructionString);
         } catch (Exception e) {
             throw new AmazonClientException("Error parsing JSON instruction file: " + e.getMessage());
         }
     }

    
Converts the contents of an input stream to a String

Deprecated:
no longer used and will be removed in the future
 
     @Deprecated
     private static String convertStreamToString(InputStream inputStreamthrows IOException {
         if (inputStream == null) {
             return "";
         }else {
             StringBuilder stringBuilder = new StringBuilder();
             String line;
             try {
                 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                 while ((line = reader.readLine()) != null) {
                     stringBuilder.append(line);
                 }
             } finally {
                inputStream.close();
            }
            return stringBuilder.toString();
        }
    }

    
Takes the position of the leftmost desired byte of a user specified range and returns the position of the start of the previous cipher block, or returns 0 if the leftmost byte is in the first cipher block.

Deprecated:
no longer used and will be removed in the future
    private static long getCipherBlockLowerBound(long leftmostBytePosition) {
        long cipherBlockSize = .;
        long offset = leftmostBytePosition % cipherBlockSize;
        long lowerBound = leftmostBytePosition - offset - cipherBlockSize;
        if (lowerBound < 0) {
            return 0;
        } else {
            return lowerBound;
        }
    }

    
Takes the position of the rightmost desired byte of a user specified range and returns the position of the end of the following cipher block.

Deprecated:
no longer used and will be removed in the future
    private static long getCipherBlockUpperBound(long rightmostBytePosition) {
        long cipherBlockSize = .;
        long offset = cipherBlockSize - (rightmostBytePosition % cipherBlockSize);
        return rightmostBytePosition + offset + cipherBlockSize;
    }
New to GrepCode? Check out our FAQ X