Refine search
/** * Converts a Base64-encoded string to the original byte data. * * @param b64Data * a Base64-encoded string to decode. * * @return bytes decoded from a Base64 string. */ public static byte[] fromBase64(String b64Data) { return b64Data == null ? null : Base64.decode(b64Data); }
@Override public void beforeRequest(Request<?> request) { AmazonWebServiceRequest originalRequest = request.getOriginalRequest(); if (originalRequest instanceof ImportKeyPairRequest) { ImportKeyPairRequest importKeyPairRequest = (ImportKeyPairRequest)originalRequest; String publicKeyMaterial = importKeyPairRequest.getPublicKeyMaterial(); String encodedKeyMaterial = Base64.encodeAsString(publicKeyMaterial.getBytes()); request.addParameter("PublicKeyMaterial", encodedKeyMaterial); request.addParameter("LaunchSpecification.SecurityGroup." + groupNameCount++, groupName);
ObjectMetadata meta = new ObjectMetadata(); if (mSseEnabled) { meta.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION); meta.setContentMD5(new String(Base64.encode(mHash.digest()))); meta.setContentLength(mFile.length());
} else { metadata.setContentLength(file.length()); final boolean calculateMD5 = metadata.getContentMD5() == null; if (metadata.getContentType() == null) { try { Request<PutObjectRequest> request = createRequest(bucketName, key, putObjectRequest, HttpMethodName.PUT); request.addHandlerContext(HandlerContextKey.OPERATION_NAME, "PutObject"); AmazonWebServiceRequest awsreq = request.getOriginalRequest(); addAclHeaders(request, putObjectRequest.getAccessControlList()); } else if ( putObjectRequest.getCannedAcl() != null ) { request.addHeader(Headers.S3_CANNED_ACL, putObjectRequest.getCannedAcl().toString()); String contentMd5 = metadata.getContentMD5(); if (md5DigestStream != null) { contentMd5 = Base64.encodeAsString(md5DigestStream.getMd5Digest()); throw new SdkClientException( "Unable to verify integrity of data upload. " + "Client calculated content hash (contentMD5: "
public String writeToS3(final String accountId, final String region, final Date instanceBootTime, final String logData, final String logType, final String instanceId) throws IOException { String fileName = null; DateTime dateTime = new DateTime(instanceBootTime, UTC); String keyName = Paths.get( accountId, region, dateTime.toString("YYYY"), dateTime.toString("MM"), dateTime.toString("dd"), instanceId + "-" + dateTime).toString(); switch (LogType.valueOf(logType)) { case USER_DATA: fileName = TAUPAGE_YAML; break; case AUDIT_LOG: fileName = AUDIT_LOG_FILE_NAME + new DateTime(UTC) + LOG_GZ; break; default: logger.error("Wrong logType given: " + logType); break; } ObjectMetadata metadata = new ObjectMetadata(); byte[] decodedLogData = Base64.decode(logData); metadata.setContentLength(decodedLogData.length); InputStream stream = new ByteArrayInputStream(decodedLogData); putObjectToS3(bucketName, fileName, keyName, metadata, stream); return Paths.get(bucketName, keyName, fileName).toString(); }
/** * Initializes multipart upload. */ private void initMultiPartUpload() throws IOException { // Generate the object metadata by setting server side encryption, md5 checksum, // and encoding as octet stream since no assumptions are made about the file type ObjectMetadata meta = new ObjectMetadata(); if (mSseEnabled) { meta.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION); } if (mHash != null) { meta.setContentMD5(Base64.encodeAsString(mHash.digest())); } meta.setContentType(Mimetypes.MIMETYPE_OCTET_STREAM); AmazonClientException lastException; InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(mBucketName, mKey).withObjectMetadata(meta); do { try { mUploadId = mClient.initiateMultipartUpload(initRequest).getUploadId(); return; } catch (AmazonClientException e) { lastException = e; } } while (mRetryPolicy.attempt()); // This point is only reached if the operation failed more // than the allowed retry count throw new IOException("Unable to init multipart upload to " + mKey, lastException); }
private boolean walk(Iterator<S3ObjectSummary> iter, ObjectId file, String path) throws IOException { byte[] content; byte[] newHash; LOG.debug("Start processing file: {}", path); try (DigestInputStream is = new DigestInputStream(repository.open(file).openStream(), DigestUtils.getMd5Digest())) { // Get content content = IOUtils.toByteArray(is); // Get hash newHash = is.getMessageDigest().digest(); } if (isUploadFile(iter, path, Hex.encodeHexString(newHash))) { LOG.info("Uploading file: {}", path); ObjectMetadata bucketMetadata = new ObjectMetadata(); bucketMetadata.setContentMD5(Base64.encodeAsString(newHash)); bucketMetadata.setContentLength(content.length); // Give Tika a few hints for the content detection Metadata tikaMetadata = new Metadata(); tikaMetadata.set(Metadata.RESOURCE_NAME_KEY, FilenameUtils.getName(FilenameUtils.normalize(path))); // Fire! try (InputStream bis = TikaInputStream.get(content, tikaMetadata)) { bucketMetadata.setContentType(TIKA_DETECTOR.detect(bis, tikaMetadata).toString()); s3.putObject(bucket.getName(), path, bis, bucketMetadata); return true; } } LOG.info("Skipping file (same checksum): {}", path); return false; }
/** * Computes an RFC 2104-compliant HMAC signature for an array of bytes and * returns the result as a Base64 encoded string. */ protected String signAndBase64Encode(byte[] data, String key, SigningAlgorithm algorithm) throws SdkClientException { try { byte[] signature = sign(data, key.getBytes(UTF8), algorithm); return Base64.encodeAsString(signature); } catch (Exception e) { throw new SdkClientException( "Unable to calculate a request signature: " + e.getMessage(), e); } }
AWSKMS kms) { Map<String, String> userMeta = metadata.getUserMetadata(); String b64key = userMeta.get(Headers.CRYPTO_KEY_V2); if (b64key == null) { b64key = userMeta.get(Headers.CRYPTO_KEY); if (b64key == null) throw new SdkClientException( "Content encrypting key not found."); byte[] cekWrapped = Base64.decode(b64key); byte[] iv = Base64.decode(userMeta.get(Headers.CRYPTO_IV)); if (cekWrapped == null || iv == null) { throw new SdkClientException( "Content encrypting key or IV not found."); throw new SdkClientException( "Unable to retrieve the client encryption materials");
protected void doUpload(S3BlobStore blobStore, String bucketName, String blobName, InputStream is, int length, boolean serverSideEncryption) throws AmazonS3Exception { ObjectMetadata md = new ObjectMetadata(); if (serverSideEncryption) { md.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION); } md.setContentLength(length); InputStream inputStream = is; // We try to compute a MD5 while reading it MessageDigest messageDigest; try { messageDigest = MessageDigest.getInstance("MD5"); inputStream = new DigestInputStream(is, messageDigest); } catch (NoSuchAlgorithmException impossible) { // Every implementation of the Java platform is required to support MD5 (see MessageDigest) throw new RuntimeException(impossible); } PutObjectResult putObjectResult = blobStore.client().putObject(bucketName, blobName, inputStream, md); String localMd5 = Base64.encodeAsString(messageDigest.digest()); String remoteMd5 = putObjectResult.getContentMd5(); if (!localMd5.equals(remoteMd5)) { logger.debug("MD5 local [{}], remote [{}] are not equal...", localMd5, remoteMd5); throw new AmazonS3Exception("MD5 local [" + localMd5 + "], remote [" + remoteMd5 + "] are not equal..."); } }
b64key = instFile.get(Headers.CRYPTO_KEY); if (b64key == null) throw new SdkClientException( "Content encrypting key not found."); byte[] cekWrapped = Base64.decode(b64key); byte[] iv = Base64.decode(instFile.get(Headers.CRYPTO_IV)); if (cekWrapped == null || iv == null) { throw new SdkClientException( "Necessary encryption info not found in the instruction file " + instFile); : kekMaterialAccessor.getEncryptionMaterials(merged); if (materials == null) { throw new SdkClientException( "Unable to retrieve the encryption materials that originally " + "encrypted object corresponding to instruction file "
private static void populateSourceSSE_C(Request<?> request, SSECustomerKey sseKey) { if (sseKey == null) return; // Populate the SSE-C parameters for the source object addHeaderIfNotNull(request, Headers.COPY_SOURCE_SERVER_SIDE_ENCRYPTION_CUSTOMER_ALGORITHM, sseKey.getAlgorithm()); addHeaderIfNotNull(request, Headers.COPY_SOURCE_SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY, sseKey.getKey()); addHeaderIfNotNull(request, Headers.COPY_SOURCE_SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY_MD5, sseKey.getMd5()); // Calculate the MD5 hash of the encryption key and fill it in the // header, if the user didn't specify it in the metadata if (sseKey.getKey() != null && sseKey.getMd5() == null) { String encryptionKey_b64 = sseKey.getKey(); byte[] encryptionKey = Base64.decode(encryptionKey_b64); request.addHeader(Headers.COPY_SOURCE_SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY_MD5, Md5Utils.md5AsBase64(encryptionKey)); } }
/** * Returns the MD5 in base64 for the given file. */ public static String md5AsBase64(File file) throws FileNotFoundException, IOException { return Base64.encodeAsString(computeMD5Hash(file)); } }
/** * Returns the metadata in backward compatibility (old) format, so it can be * read by older version of the AWS SDK. */ private ObjectMetadata toObjectMetadataEO(ObjectMetadata metadata) { // If we generated a symmetric key to encrypt the data, store it in the // object metadata. byte[] encryptedCEK = getEncryptedCEK(); metadata.addUserMetadata(Headers.CRYPTO_KEY, Base64.encodeAsString(encryptedCEK)); // Put the cipher initialization vector (IV) into the object metadata byte[] iv = cipherLite.getIV(); metadata.addUserMetadata(Headers.CRYPTO_IV, Base64.encodeAsString(iv)); // Put the materials description into the object metadata as JSON metadata.addUserMetadata(Headers.MATERIALS_DESCRIPTION, kekMaterialDescAsJson()); return metadata; }
/** * 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. */ private static byte[] getCryptoBytesFromMetadata(String headerName, ObjectMetadata metadata) { Map<String, String> userMetadata = metadata.getUserMetadata(); if (userMetadata == null || !userMetadata.containsKey(headerName)) { return null; } else { // Convert Base64 bytes to binary data. return Base64.decode(userMetadata.get(headerName)); } }
mac.update(deviceGroupKey.getBytes(StringUtils.UTF8)); mac.update(deviceKey.getBytes(StringUtils.UTF8)); final byte[] secretBlock = Base64.decode(challenge.getChallengeParameters() .get(CognitoServiceConstants.CHLG_PARAM_SECRET_BLOCK)); mac.update(secretBlock); .get(CognitoServiceConstants.CHLG_PARAM_SECRET_BLOCK)); srpAuthResponses.put(CognitoServiceConstants.CHLG_RESP_PASSWORD_CLAIM_SIGNATURE, new String(Base64.encode(hmac), StringUtils.UTF8)); srpAuthResponses.put(CognitoServiceConstants.CHLG_RESP_TIMESTAMP, dateString); srpAuthResponses.put(CognitoServiceConstants.CHLG_RESP_USERNAME, usernameInternal);
@Override public String toString() { return Base64.encodeAsString(value); } }
@Test public void testCodecConsistency() throws NoSuchAlgorithmException, UnsupportedEncodingException { byte[] decoded = null; for (int h = 0; h < 1000; h++) { byte[] digest = MessageDigest.getInstance("SHA-1").digest( UUID.randomUUID().toString().getBytes("UTF-8")); String b64Encoded = Base64.encodeAsString(digest); decoded = Base64.decode(b64Encoded); Assert.assertTrue(Arrays.equals(decoded, digest)); } }
/** * Converts the given data to be safe for use in signed URLs for a private * distribution by using specialized Base64 encoding. */ public static String makeBytesUrlSafe(byte[] bytes) { byte[] encoded = Base64.encode(bytes); for (int i=0; i < encoded.length; i++) { switch(encoded[i]) { case '+': encoded[i] = '-'; continue; case '=': encoded[i] = '_'; continue; case '/': encoded[i] = '~'; continue; default: continue; } } return new String(encoded, UTF8); }
InputStream input = isOrig; if (metadata == null) metadata = new ObjectMetadata(); rejectNull(bucketName, "The bucket name parameter must be specified when uploading an object"); rejectNull(key, "The key parameter must be specified when uploading an object"); } else { metadata.setContentLength(file.length()); final boolean calculateMD5 = metadata.getContentMD5() == null; if (metadata.getContentType() == null) { AmazonWebServiceRequest awsreq = request.getOriginalRequest(); addAclHeaders(request, putObjectRequest.getAccessControlList()); } else if ( putObjectRequest.getCannedAcl() != null ) { request.addHeader(Headers.S3_CANNED_ACL, putObjectRequest.getCannedAcl().toString()); request.addHeader(Headers.STORAGE_CLASS, putObjectRequest.getStorageClass()); String contentMd5 = metadata.getContentMD5(); if (md5DigestStream != null) { contentMd5 = Base64.encodeAsString(md5DigestStream.getMd5Digest()); throw new SdkClientException( "Unable to verify integrity of data upload. " + "Client calculated content hash (contentMD5: "