Merge pull request #1124 from jclouds/guava-encoding-hashing

fix issue #1101: Move to Guava 14 BaseEncoding and Hashing
This commit is contained in:
Adrian Cole 2012-12-31 15:43:47 -08:00
commit 461fa51ce3
114 changed files with 675 additions and 3591 deletions

View File

@ -19,10 +19,12 @@
package org.jclouds.atmos.filters;
import static com.google.common.base.Throwables.propagate;
import static com.google.common.io.BaseEncoding.base64;
import static com.google.common.io.ByteStreams.readBytes;
import static org.jclouds.Constants.LOGGER_SIGNATURE;
import static org.jclouds.crypto.CryptoStreams.base64;
import static org.jclouds.crypto.CryptoStreams.mac;
import static org.jclouds.crypto.Macs.asByteProcessor;
import static org.jclouds.http.Uris.uriBuilder;
import static org.jclouds.util.Strings2.toInputStream;
import java.io.IOException;
import java.net.URI;
@ -36,7 +38,6 @@ import javax.inject.Singleton;
import org.jclouds.crypto.Crypto;
import org.jclouds.date.TimeStamp;
import org.jclouds.http.HttpException;
import org.jclouds.io.InputSuppliers;
import org.jclouds.location.Provider;
import org.jclouds.logging.Logger;
import org.jclouds.rest.annotations.Credential;
@ -45,6 +46,7 @@ import org.jclouds.rest.annotations.Identity;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.ByteProcessor;
/**
* Signs the EMC Atmos Online Storage request.
@ -73,7 +75,7 @@ public class ShareUrl implements Function<String, URI> {
public ShareUrl(@Identity String uid, @Credential String encodedKey,
@Provider Supplier<URI> provider, @TimeStamp javax.inject.Provider<Long> timeStampProvider, Crypto crypto) {
this.uid = uid;
this.key = base64(encodedKey);
this.key = base64().decode(encodedKey);
this.provider = provider;
this.timeStampProvider = timeStampProvider;
this.crypto = crypto;
@ -100,7 +102,8 @@ public class ShareUrl implements Function<String, URI> {
public String signString(String toSign) {
try {
return base64(mac(InputSuppliers.of(toSign), crypto.hmacSHA1(key)));
ByteProcessor<byte[]> hmacSHA1 = asByteProcessor(crypto.hmacSHA1(key));
return base64().encode(readBytes(toInputStream(toSign), hmacSHA1));
} catch (InvalidKeyException e) {
throw propagate(e);
} catch (IOException e) {

View File

@ -18,9 +18,13 @@
*/
package org.jclouds.atmos.filters;
import static com.google.common.io.BaseEncoding.base64;
import static com.google.common.io.ByteStreams.readBytes;
import static org.jclouds.Constants.LOGGER_SIGNATURE;
import static org.jclouds.crypto.Macs.asByteProcessor;
import static org.jclouds.util.Patterns.NEWLINE_PATTERN;
import static org.jclouds.util.Patterns.TWO_SPACE_PATTERN;
import static org.jclouds.util.Strings2.toInputStream;
import java.util.Set;
@ -33,14 +37,12 @@ import javax.ws.rs.core.HttpHeaders;
import org.jclouds.atmos.reference.AtmosHeaders;
import org.jclouds.crypto.Crypto;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.date.TimeStamp;
import org.jclouds.http.HttpException;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpRequestFilter;
import org.jclouds.http.HttpUtils;
import org.jclouds.http.internal.SignatureWire;
import org.jclouds.io.InputSuppliers;
import org.jclouds.logging.Logger;
import org.jclouds.rest.annotations.Credential;
import org.jclouds.rest.annotations.Identity;
@ -52,6 +54,7 @@ import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.collect.Multimaps;
import com.google.common.collect.Sets;
import com.google.common.io.ByteProcessor;
/**
* Signs the EMC Atmos Online Storage request.
@ -83,7 +86,7 @@ public class SignRequest implements HttpRequestFilter {
HttpUtils utils) {
this.signatureWire = signatureWire;
this.uid = uid;
this.key = CryptoStreams.base64(encodedKey);
this.key = base64().decode(encodedKey);
this.timeStampProvider = timeStampProvider;
this.crypto = crypto;
this.utils = utils;
@ -126,13 +129,12 @@ public class SignRequest implements HttpRequestFilter {
}
public String signString(String toSign) {
String signature;
try {
signature = CryptoStreams.base64(CryptoStreams.mac(InputSuppliers.of(toSign), crypto.hmacSHA1(key)));
ByteProcessor<byte[]> hmacSHA1 = asByteProcessor(crypto.hmacSHA1(key));
return base64().encode(readBytes(toInputStream(toSign), hmacSHA1));
} catch (Exception e) {
throw new HttpException("error signing request", e);
}
return signature;
}
private void appendMethod(HttpRequest request, StringBuilder toSign) {

View File

@ -19,6 +19,7 @@
package org.jclouds.atmos.functions;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.io.BaseEncoding.base16;
import java.util.Map;
@ -28,7 +29,6 @@ import javax.inject.Singleton;
import org.jclouds.atmos.domain.FileType;
import org.jclouds.atmos.domain.SystemMetadata;
import org.jclouds.atmos.reference.AtmosHeaders;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.date.DateService;
import org.jclouds.http.HttpResponse;
@ -57,7 +57,7 @@ public class ParseSystemMetadataFromHeaders implements Function<HttpResponse, Sy
metaMap.put(entrySplit[0], entrySplit[1]);
}
assert metaMap.size() >= 12 : String.format("Should be 12 entries in %s", metaMap);
byte[] md5 = metaMap.containsKey("content-md5") ? CryptoStreams.hex(metaMap.get("content-md5")) : null;
byte[] md5 = metaMap.containsKey("content-md5") ? base16().lowerCase().decode(metaMap.get("content-md5")) : null;
return new SystemMetadata(md5, dateService.iso8601SecondsDateParse(checkNotNull(metaMap.get("atime"), "atime")),
dateService.iso8601SecondsDateParse(checkNotNull(metaMap.get("ctime"), "ctime")), checkNotNull(
metaMap.get("gid"), "gid"), dateService.iso8601SecondsDateParse(checkNotNull(metaMap.get("itime"),

View File

@ -18,11 +18,11 @@
*/
package org.jclouds.atmos.functions;
import static com.google.common.io.BaseEncoding.base16;
import static org.testng.Assert.assertEquals;
import org.jclouds.atmos.domain.FileType;
import org.jclouds.atmos.domain.SystemMetadata;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.date.DateService;
import org.jclouds.date.internal.SimpleDateFormatDateService;
import org.testng.annotations.Test;
@ -37,8 +37,7 @@ import com.google.inject.Guice;
@Test(groups = "unit")
public class ParseSystemMetadataFromHeadersTest {
static final DateService dateService = new SimpleDateFormatDateService();
static final SystemMetadata EXPECTED = new SystemMetadata(CryptoStreams.hex("1f3870be274f6c49b3e31a0c6728957f"),
static final SystemMetadata EXPECTED = new SystemMetadata(base16().lowerCase().decode("1f3870be274f6c49b3e31a0c6728957f"),
dateService.iso8601SecondsDateParse("2009-10-12T16:09:42Z"),
dateService.iso8601SecondsDateParse("2009-10-19T04:37:00Z"), "rootr",
dateService.iso8601SecondsDateParse("2009-10-12T16:09:42Z"),

View File

@ -21,6 +21,7 @@ package org.jclouds.cloudservers.options;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.io.BaseEncoding.base64;
import java.util.List;
import java.util.Map;
@ -29,7 +30,6 @@ import java.util.Map.Entry;
import javax.inject.Inject;
import org.jclouds.cloudservers.domain.Addresses;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.http.HttpRequest;
import org.jclouds.rest.MapBinder;
import org.jclouds.rest.binders.BindToJsonPayload;
@ -54,7 +54,7 @@ public class CreateServerOptions implements MapBinder {
public File(String path, byte[] contents) {
this.path = checkNotNull(path, "path");
this.contents = CryptoStreams.base64(checkNotNull(contents, "contents"));
this.contents = base64().encode(checkNotNull(contents, "contents"));
checkArgument(path.getBytes().length < 255, String.format(
"maximum length of path is 255 bytes. Path specified %s is %d bytes", path, path.getBytes().length));
checkArgument(contents.length < 10 * 1024, String.format(

View File

@ -20,9 +20,10 @@ package org.jclouds.cloudstack.filters;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Throwables.propagate;
import static com.google.common.io.BaseEncoding.base64;
import static com.google.common.io.ByteStreams.readBytes;
import static org.jclouds.Constants.LOGGER_SIGNATURE;
import static org.jclouds.crypto.CryptoStreams.base64;
import static org.jclouds.crypto.CryptoStreams.mac;
import static org.jclouds.crypto.Macs.asByteProcessor;
import static org.jclouds.http.Uris.uriBuilder;
import static org.jclouds.http.utils.Queries.encodeQueryLine;
import static org.jclouds.http.utils.Queries.queryParser;
@ -41,7 +42,6 @@ import org.jclouds.http.HttpException;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpUtils;
import org.jclouds.http.internal.SignatureWire;
import org.jclouds.io.InputSuppliers;
import org.jclouds.logging.Logger;
import org.jclouds.rest.RequestSigner;
import org.jclouds.rest.annotations.Credential;
@ -51,6 +51,7 @@ import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Multimap;
import com.google.common.collect.TreeMultimap;
import com.google.common.io.ByteProcessor;
/**
*
@ -99,10 +100,11 @@ public class QuerySigner implements AuthenticationFilter, RequestSigner {
}
@VisibleForTesting
public String sign(String stringToSign) {
public String sign(String toSign) {
String signature;
try {
signature = base64(mac(InputSuppliers.of(stringToSign), crypto.hmacSHA1(secretKey.getBytes())));
ByteProcessor<byte[]> hmacSHA1 = asByteProcessor(crypto.hmacSHA1(secretKey.getBytes()));
signature = base64().encode(readBytes(toInputStream(toSign), hmacSHA1));
if (signatureWire.enabled())
signatureWire.input(toInputStream(signature));
return signature;

View File

@ -18,6 +18,10 @@
*/
package org.jclouds.cloudstack.functions;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.hash.Hashing.md5;
import static com.google.common.io.BaseEncoding.base16;
import java.io.File;
import javax.inject.Inject;
@ -25,7 +29,6 @@ import javax.inject.Singleton;
import org.jclouds.cloudstack.domain.LoginResponse;
import org.jclouds.cloudstack.features.SessionClient;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.domain.Credentials;
import com.google.common.base.Function;
@ -50,9 +53,7 @@ public class LoginWithPasswordCredentials implements Function<Credentials, Login
username = domainUsername.getName();
domain = domainUsername.getParent();
}
String hashedPassword = CryptoStreams.md5Hex(input.credential);
String hashedPassword = base16().lowerCase().encode(md5().hashString(input.credential, UTF_8).asBytes());
return client.loginUserInDomainWithHashOfPassword(username, domain, hashedPassword);
}

View File

@ -18,20 +18,22 @@
*/
package org.jclouds.cloudstack.functions;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.io.BaseEncoding.base64;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.KeySpec;
import java.util.regex.Pattern;
import javax.crypto.Cipher;
import org.jclouds.cloudstack.domain.EncryptedPasswordAndPrivateKey;
import org.jclouds.crypto.Crypto;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.crypto.Pems;
import org.jclouds.domain.LoginCredentials;
import org.jclouds.javax.annotation.Nullable;
import com.google.common.base.Charsets;
import com.google.common.base.Function;
import com.google.common.base.Throwables;
import com.google.inject.Inject;
@ -51,12 +53,13 @@ public class WindowsLoginCredentialsFromEncryptedData implements Function<Encryp
public WindowsLoginCredentialsFromEncryptedData(Crypto crypto) {
this.crypto = crypto;
}
private static final Pattern whitespace = Pattern.compile("\\s");
@Override
public LoginCredentials apply(@Nullable EncryptedPasswordAndPrivateKey dataAndKey) {
if (dataAndKey == null)
return null;
try {
KeySpec keySpec = Pems.privateKeySpec(dataAndKey.getPrivateKey());
KeyFactory kf = crypto.rsaKeyFactory();
@ -64,9 +67,9 @@ public class WindowsLoginCredentialsFromEncryptedData implements Function<Encryp
Cipher cipher = crypto.cipher("RSA");
cipher.init(Cipher.DECRYPT_MODE, privKey);
byte[] cipherText = CryptoStreams.base64(dataAndKey.getEncryptedPassword());
byte[] cipherText = base64().decode(whitespace.matcher(dataAndKey.getEncryptedPassword()).replaceAll(""));
byte[] plainText = cipher.doFinal(cipherText);
String password = new String(plainText, Charsets.US_ASCII);
String password = new String(plainText, UTF_8);
return LoginCredentials.builder()
.user("Administrator")

View File

@ -20,11 +20,10 @@ package org.jclouds.cloudstack.options;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.io.BaseEncoding.base64;
import java.util.Map;
import org.jclouds.crypto.CryptoStreams;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
@ -215,7 +214,7 @@ public class DeployVirtualMachineOptions extends AccountInDomainOptions {
int length = checkNotNull(unencodedData, "unencodedData").length;
checkArgument(length > 0, "userData cannot be empty");
checkArgument(length <= 2 * 1024, "userData cannot be larger than 2kb");
this.queryParameters.replaceValues("userdata", ImmutableSet.of(CryptoStreams.base64(unencodedData)));
this.queryParameters.replaceValues("userdata", ImmutableSet.of(base64().encode(unencodedData)));
return this;
}

View File

@ -18,13 +18,15 @@
*/
package org.jclouds.cloudstack.features;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.hash.Hashing.md5;
import static com.google.common.io.BaseEncoding.base16;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import org.jclouds.cloudstack.CloudStackGlobalClient;
import org.jclouds.cloudstack.domain.Account;
import org.jclouds.cloudstack.internal.BaseCloudStackClientLiveTest;
import org.jclouds.crypto.CryptoStreams;
import org.testng.annotations.Test;
/**
@ -36,9 +38,8 @@ import org.testng.annotations.Test;
public class GlobalAccountClientLiveTest extends BaseCloudStackClientLiveTest {
public static Account createTestAccount(CloudStackGlobalClient client, String prefix) {
return client.getAccountClient().createAccount(
prefix + "-account", Account.Type.USER, "dummy@example.com",
"First", "Last", CryptoStreams.md5Hex("password"));
return client.getAccountClient().createAccount(prefix + "-account", Account.Type.USER, "dummy@example.com",
"First", "Last", base16().lowerCase().encode(md5().hashString("password", UTF_8).asBytes()));
}
@Test

View File

@ -18,7 +18,10 @@
*/
package org.jclouds.cloudstack.features;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.hash.Hashing.md5;
import static com.google.common.io.BaseEncoding.base16;
import static org.jclouds.cloudstack.features.GlobalAccountClientLiveTest.createTestAccount;
import static org.jclouds.cloudstack.options.UpdateUserOptions.Builder.userName;
import static org.testng.Assert.assertEquals;
@ -35,7 +38,6 @@ import org.jclouds.cloudstack.domain.ApiKeyPair;
import org.jclouds.cloudstack.domain.User;
import org.jclouds.cloudstack.internal.BaseCloudStackClientLiveTest;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.crypto.CryptoStreams;
import org.testng.annotations.Test;
/**
@ -45,8 +47,8 @@ import org.testng.annotations.Test;
public class GlobalUserClientLiveTest extends BaseCloudStackClientLiveTest {
public static User createTestUser(CloudStackGlobalClient client, Account account, String prefix) {
return client.getUserClient().createUser(prefix + "-user",
account.getName(), "dummy2@example.com", CryptoStreams.md5Hex("password"), "First", "Last");
return client.getUserClient().createUser(prefix + "-user", account.getName(), "dummy2@example.com",
base16().lowerCase().encode(md5().hashString("password", UTF_8).asBytes()), "First", "Last");
}
@Test

View File

@ -18,7 +18,9 @@
*/
package org.jclouds.cloudstack.features;
import static org.jclouds.crypto.CryptoStreams.md5Hex;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.hash.Hashing.md5;
import static com.google.common.io.BaseEncoding.base16;
import static org.testng.Assert.assertEquals;
import java.io.IOException;
@ -46,7 +48,7 @@ public class SessionClientExpectTest extends BaseCloudStackExpectTest<SessionCli
String domain = "Partners/jCloud";
String user = "jcloud";
String password = "jcl0ud";
String md5password = md5Hex(password);
String md5password = base16().lowerCase().encode(md5().hashString(password, UTF_8).asBytes());
HttpRequest request = HttpRequest.builder()
.method("GET")

View File

@ -18,6 +18,9 @@
*/
package org.jclouds.cloudstack.features;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.hash.Hashing.md5;
import static com.google.common.io.BaseEncoding.base16;
import static org.jclouds.cloudstack.features.GlobalAccountClientLiveTest.createTestAccount;
import static org.jclouds.cloudstack.features.GlobalUserClientLiveTest.createTestUser;
import static org.testng.Assert.assertEquals;
@ -31,7 +34,6 @@ import org.jclouds.cloudstack.domain.LoginResponse;
import org.jclouds.cloudstack.domain.User;
import org.jclouds.cloudstack.internal.BaseCloudStackClientLiveTest;
import org.jclouds.cloudstack.util.ApiKeyPairs;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.rest.AuthorizationException;
import org.testng.annotations.Test;
@ -83,8 +85,8 @@ public class SessionClientLiveTest extends BaseCloudStackClientLiveTest {
}
private void checkLoginAsTheNewUser(String expectedUsername) {
LoginResponse response = globalAdminClient.getSessionClient()
.loginUserInDomainWithHashOfPassword(expectedUsername, "", CryptoStreams.md5Hex("password"));
LoginResponse response = globalAdminClient.getSessionClient().loginUserInDomainWithHashOfPassword(
expectedUsername, "", base16().lowerCase().encode(md5().hashString("password", UTF_8).asBytes()));
assertNotNull(response);
assertNotNull(response.getSessionKey());

View File

@ -18,7 +18,9 @@
*/
package org.jclouds.cloudstack.internal;
import static org.jclouds.crypto.CryptoStreams.md5Hex;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.hash.Hashing.md5;
import static com.google.common.io.BaseEncoding.base16;
import static org.jclouds.util.Strings2.urlEncode;
import java.util.Properties;
@ -61,7 +63,7 @@ public abstract class BaseCloudStackExpectTest<S> extends BaseRestClientExpectTe
.addQueryParam("response", "json")
.addQueryParam("command", "login")
.addQueryParam("username", "identity")
.addQueryParam("password", md5Hex("credential"))
.addQueryParam("password", base16().lowerCase().encode(md5().hashString("credential", UTF_8).asBytes()))
.addQueryParam("domain", "")
.addHeader("Accept", "application/json")
.build();

View File

@ -18,17 +18,17 @@
*/
package org.jclouds.ec2.binders;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.io.BaseEncoding.base64;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.aws.filters.FormSigner;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.http.HttpRequest;
import org.jclouds.rest.Binder;
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableMultimap.Builder;
@ -48,7 +48,7 @@ public class BindS3UploadPolicyAndSignature implements Binder {
@SuppressWarnings("unchecked")
@Override
public <R extends HttpRequest> R bindToRequest(R request, Object input) {
String encodedJson = CryptoStreams.base64(checkNotNull(input, "json").toString().getBytes(Charsets.UTF_8));
String encodedJson = base64().encode(checkNotNull(input, "json").toString().getBytes(UTF_8));
Builder<String, String> builder = ImmutableMultimap.builder();
builder.put("Storage.S3.UploadPolicy", encodedJson);
String signature = signer.sign(encodedJson);

View File

@ -18,6 +18,8 @@
*/
package org.jclouds.ec2.compute.functions;
import static com.google.common.io.BaseEncoding.base64;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.KeySpec;
@ -26,7 +28,6 @@ import javax.crypto.Cipher;
import javax.inject.Inject;
import org.jclouds.crypto.Crypto;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.crypto.Pems;
import org.jclouds.domain.LoginCredentials;
import org.jclouds.ec2.compute.domain.PasswordDataAndPrivateKey;
@ -64,7 +65,7 @@ public class WindowsLoginCredentialsFromEncryptedData implements Function<Passwo
Cipher cipher = crypto.cipher("RSA");
cipher.init(Cipher.DECRYPT_MODE, privKey);
byte[] cipherText = CryptoStreams.base64(dataAndKey.getPasswordData().getPasswordData());
byte[] cipherText = base64().decode(dataAndKey.getPasswordData().getPasswordData());
byte[] plainText = cipher.doFinal(cipherText);
String password = new String(plainText, Charsets.US_ASCII);

View File

@ -20,11 +20,10 @@ package org.jclouds.ec2.functions;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.io.BaseEncoding.base64;
import javax.inject.Singleton;
import org.jclouds.crypto.CryptoStreams;
import com.google.common.base.Function;
/**
@ -38,10 +37,9 @@ public class ConvertUnencodedBytesToBase64EncodedString implements Function<Obje
@Override
public String apply(Object from) {
checkArgument(checkNotNull(from, "input") instanceof byte[], "this binder is only valid for byte []!");
byte[] unencodedData = (byte[]) from;
checkArgument(checkNotNull(unencodedData, "unencodedData").length <= 16 * 1024,
"userData cannot be larger than 16kb");
return CryptoStreams.base64(unencodedData);
"userData cannot be larger than 16kb");
return base64().encode(unencodedData);
}
}

View File

@ -18,15 +18,15 @@
*/
package org.jclouds.ec2.functions;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.io.BaseEncoding.base64;
import javax.inject.Singleton;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.util.Predicates2;
import com.google.common.base.Charsets;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
@ -46,9 +46,8 @@ public class EncodedRSAPublicKeyToBase64 implements Function<Object, String> {
@Override
public String apply(Object from) {
checkNotNull(from, "input");
String fromString = from.toString();
String fromString = checkNotNull(from, "input").toString();
checkArgument(ALLOWED_MARKERS.apply(fromString), "must be a ssh public key, conforming to %s ", ALLOWED_MARKERS);
return CryptoStreams.base64(fromString.getBytes(Charsets.UTF_8));
return base64().encode(fromString.getBytes(UTF_8));
}
}

View File

@ -20,10 +20,10 @@ package org.jclouds.ec2.options;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.io.BaseEncoding.base64;
import java.util.Set;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.ec2.domain.BlockDeviceMapping;
import org.jclouds.ec2.domain.InstanceType;
import org.jclouds.ec2.options.internal.BaseEC2RequestOptions;
@ -90,7 +90,7 @@ public class RunInstancesOptions extends BaseEC2RequestOptions {
int length = checkNotNull(unencodedData, "unencodedData").length;
checkArgument(length > 0, "userData cannot be empty");
checkArgument(length <= 16 * 1024, "userData cannot be larger than 16kb");
formParameters.put("UserData", CryptoStreams.base64(unencodedData));
formParameters.put("UserData", base64().encode(unencodedData));
return this;
}

View File

@ -18,10 +18,12 @@
*/
package org.jclouds.ec2.xml;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.http.functions.ParseSax;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.io.BaseEncoding.base64;
import com.google.common.base.Charsets;
import java.util.regex.Pattern;
import org.jclouds.http.functions.ParseSax;
/**
* @author Andrew Kennedy
@ -38,10 +40,12 @@ public class GetConsoleOutputResponseHandler extends ParseSax.HandlerWithResult<
return output;
}
private static final Pattern whitespace = Pattern.compile("\\s");
@Override
public void endElement(String uri, String name, String qName) {
if (qName.equalsIgnoreCase("output")) {
this.output = new String(CryptoStreams.base64(currentText.toString().trim()), Charsets.UTF_8);
this.output = new String(base64().decode(whitespace.matcher(currentText).replaceAll("")), UTF_8);
}
currentText = new StringBuilder();
}

View File

@ -18,9 +18,8 @@
*/
package org.jclouds.ec2.xml;
import org.jclouds.crypto.CryptoStreams;
import com.google.common.base.Charsets;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.io.BaseEncoding.base64;
/**
*
@ -32,6 +31,9 @@ public class UnencodeStringValueHandler extends StringValueHandler {
@Override
public String getResult() {
return super.getResult() == null ? null : new String(CryptoStreams.base64(super.getResult()), Charsets.UTF_8);
String result = super.getResult();
if (result != null)
result = new String(base64().decode(result), UTF_8);
return result;
}
}

View File

@ -18,6 +18,7 @@
*/
package org.jclouds.ec2.compute.strategy;
import static com.google.common.io.BaseEncoding.base64;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
@ -36,7 +37,6 @@ import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.Template;
import org.jclouds.compute.functions.GroupNamingConvention;
import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.domain.LoginCredentials;
import org.jclouds.ec2.compute.domain.EC2HardwareBuilder;
import org.jclouds.ec2.compute.domain.RegionAndName;
@ -199,7 +199,8 @@ public class CreateKeyPairAndSecurityGroupsAsNeededAndReturnRunOptionsTest {
assertEquals(
customize.buildFormParameters().entries(),
ImmutableMultimap.<String, String> of("InstanceType", size.getProviderId(), "SecurityGroup.1", "group",
"KeyName", systemGeneratedKeyPairName, "UserData", CryptoStreams.base64("hello".getBytes())).entries());
"KeyName", systemGeneratedKeyPairName, "UserData", base64().encode("hello".getBytes())).entries());
assertEquals(customize.buildRequestHeaders(), ImmutableMultimap.<String, String> of());
assertEquals(customize.buildStringPayload(), null);

View File

@ -75,10 +75,8 @@ public class DescribeInstanceAttributeTest extends BaseHandlerTest {
public void testUnencodeStringValueHandler() {
InputStream is = getClass().getResourceAsStream("/userData.xml");
UnencodeStringValueHandler handler = injector.getInstance(UnencodeStringValueHandler.class);
String result = factory.create(handler).parse(is);
assertEquals(result, "#!/bin/bash\n");
}
}

View File

@ -3,6 +3,6 @@
<requestId>0d0ca353-fba3-402c-8516-005e2b41d0ba</requestId>
<instanceId>i-c72d0baf</instanceId>
<userData>
<value>IyEvYmluL2Jhc2gKc</value>
<value>IyEvYmluL2Jhc2gK</value>
</userData>
</DescribeInstanceAttributeResponse>

View File

@ -19,6 +19,7 @@
package org.jclouds.filesystem.strategy.internal;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.io.BaseEncoding.base16;
import java.io.File;
import java.io.IOException;
@ -29,19 +30,10 @@ import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
import com.google.common.base.Function;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;
import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
import org.jclouds.blobstore.LocalStorageStrategy;
import org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.domain.BlobBuilder;
import org.jclouds.blobstore.options.ListContainerOptions;
import org.jclouds.crypto.Crypto;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.domain.Location;
import org.jclouds.filesystem.predicates.validators.FilesystemBlobKeyValidator;
import org.jclouds.filesystem.predicates.validators.FilesystemContainerNameValidator;
@ -52,6 +44,13 @@ import org.jclouds.io.Payloads;
import org.jclouds.logging.Logger;
import org.jclouds.rest.annotations.ParamValidators;
import com.google.common.base.Function;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;
import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
/**
*
* @author Alfredo "Rainbowbreeze" Morresi
@ -67,20 +66,17 @@ public class FilesystemStorageStrategyImpl implements LocalStorageStrategy {
protected final String baseDirectory;
protected final FilesystemContainerNameValidator filesystemContainerNameValidator;
protected final FilesystemBlobKeyValidator filesystemBlobKeyValidator;
private final Crypto crypto;
@Inject
protected FilesystemStorageStrategyImpl(Provider<BlobBuilder> blobBuilders,
@Named(FilesystemConstants.PROPERTY_BASEDIR) String baseDir,
FilesystemContainerNameValidator filesystemContainerNameValidator,
FilesystemBlobKeyValidator filesystemBlobKeyValidator,
Crypto crypto) {
FilesystemBlobKeyValidator filesystemBlobKeyValidator) {
this.blobBuilders = checkNotNull(blobBuilders, "filesystem storage strategy blobBuilders");
this.baseDirectory = checkNotNull(baseDir, "filesystem storage strategy base directory");
this.filesystemContainerNameValidator = checkNotNull(filesystemContainerNameValidator,
"filesystem container name validator");
this.filesystemBlobKeyValidator = checkNotNull(filesystemBlobKeyValidator, "filesystem blob key validator");
this.crypto = crypto;
}
@Override
@ -188,7 +184,7 @@ public class FilesystemStorageStrategyImpl implements LocalStorageStrategy {
}
Blob blob = builder.build();
if (blob.getPayload().getContentMetadata().getContentMD5() != null)
blob.getMetadata().setETag(CryptoStreams.hex(blob.getPayload().getContentMetadata().getContentMD5()));
blob.getMetadata().setETag(base16().lowerCase().encode(blob.getPayload().getContentMetadata().getContentMD5()));
return blob;
}
@ -207,8 +203,8 @@ public class FilesystemStorageStrategyImpl implements LocalStorageStrategy {
payload = Payloads.newPayload(ByteStreams.toByteArray(payload));
Files.copy(payload, outputFile);
}
Payloads.calculateMD5(payload, crypto.md5());
String eTag = CryptoStreams.hex(payload.getContentMetadata().getContentMD5());
Payloads.calculateMD5(payload);
String eTag = base16().lowerCase().encode(payload.getContentMetadata().getContentMD5());
return eTag;
} catch (IOException ex) {
if (outputFile != null) {

View File

@ -18,6 +18,7 @@
*/
package org.jclouds.filesystem;
import static com.google.common.io.BaseEncoding.base16;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
@ -31,7 +32,6 @@ import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
@ -48,7 +48,6 @@ import org.jclouds.blobstore.domain.StorageMetadata;
import org.jclouds.blobstore.domain.StorageType;
import org.jclouds.blobstore.options.GetOptions;
import org.jclouds.blobstore.options.ListContainerOptions;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.filesystem.reference.FilesystemConstants;
import org.jclouds.filesystem.util.Utils;
import org.jclouds.filesystem.utils.TestUtils;
@ -62,7 +61,6 @@ import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import com.google.common.base.Charsets;
import com.google.common.collect.Sets;
import com.google.common.io.ByteStreams;
import com.google.common.io.Closeables;
@ -626,7 +624,7 @@ public class FilesystemAsyncBlobStoreTest {
assertEquals(metadata.getName(), BLOB_KEY, "Wrong blob name");
assertEquals(metadata.getType(), StorageType.BLOB, "Wrong blob type");
assertEquals(metadata.getContentMetadata().getContentType(), "application/unknown", "Wrong blob content-type");
assertEquals(CryptoStreams.hex(metadata.getContentMetadata().getContentMD5()), metadata.getETag(),
assertEquals(base16().lowerCase().encode(metadata.getContentMetadata().getContentMD5()), metadata.getETag(),
"Wrong blob MD5");
assertEquals(metadata.getLocation(), null, "Wrong blob location");
assertEquals(metadata.getProviderId(), null, "Wrong blob provider id");

View File

@ -37,7 +37,6 @@ import org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.domain.BlobBuilder;
import org.jclouds.blobstore.domain.internal.BlobBuilderImpl;
import org.jclouds.blobstore.options.ListContainerOptions;
import org.jclouds.encryption.internal.JCECrypto;
import org.jclouds.filesystem.predicates.validators.internal.FilesystemBlobKeyValidatorImpl;
import org.jclouds.filesystem.predicates.validators.internal.FilesystemContainerNameValidatorImpl;
import org.jclouds.filesystem.utils.TestUtils;
@ -79,14 +78,10 @@ public class FilesystemStorageStrategyImplTest {
storageStrategy = new FilesystemStorageStrategyImpl(new Provider<BlobBuilder>() {
@Override
public BlobBuilder get() {
try {
return new BlobBuilderImpl(new JCECrypto());
} catch (Exception e) {
return null;
}
return new BlobBuilderImpl();
}
}, TestUtils.TARGET_BASE_DIR, new FilesystemContainerNameValidatorImpl(), new FilesystemBlobKeyValidatorImpl(), new JCECrypto());
}, TestUtils.TARGET_BASE_DIR, new FilesystemContainerNameValidatorImpl(), new FilesystemBlobKeyValidatorImpl());
TestUtils.cleanDirectoryContent(TestUtils.TARGET_BASE_DIR);
}
@ -421,13 +416,9 @@ public class FilesystemStorageStrategyImplTest {
new Provider<BlobBuilder>() {
@Override
public BlobBuilder get() {
try {
return new BlobBuilderImpl(new JCECrypto());
} catch (Exception e) {
return null;
}
return new BlobBuilderImpl();
}
}, absoluteBasePath, new FilesystemContainerNameValidatorImpl(), new FilesystemBlobKeyValidatorImpl(), new JCECrypto());
}, absoluteBasePath, new FilesystemContainerNameValidatorImpl(), new FilesystemBlobKeyValidatorImpl());
TestUtils.cleanDirectoryContent(absoluteContainerPath);
String blobKey;

View File

@ -23,6 +23,7 @@ import static com.google.common.base.Objects.toStringHelper;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.io.BaseEncoding.base64;
import java.util.List;
import java.util.Map;
@ -32,7 +33,6 @@ import java.util.Set;
import javax.inject.Inject;
import javax.inject.Named;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.http.HttpRequest;
import org.jclouds.openstack.nova.v2_0.NovaApi;
import org.jclouds.rest.MapBinder;
@ -63,7 +63,7 @@ public class CreateServerOptions implements MapBinder {
public File(String path, byte[] contents) {
this.path = checkNotNull(path, "path");
this.contents = CryptoStreams.base64(checkNotNull(contents, "contents"));
this.contents = base64().encode(checkNotNull(contents, "contents"));
checkArgument(
path.getBytes().length < 255,
String.format("maximum length of path is 255 bytes. Path specified %s is %d bytes", path,
@ -184,7 +184,7 @@ public class CreateServerOptions implements MapBinder {
if (keyName != null)
server.key_name = keyName;
if (userData != null)
server.user_data = CryptoStreams.base64(userData);
server.user_data = base64().encode(userData);
if (securityGroupNames.size() > 0) {
server.securityGroupNames = Sets.newLinkedHashSet();
for (String groupName : securityGroupNames) {

View File

@ -18,9 +18,13 @@
*/
package org.jclouds.s3.filters;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.collect.Iterables.get;
import static com.google.common.io.BaseEncoding.base64;
import static com.google.common.io.ByteStreams.readBytes;
import static org.jclouds.aws.reference.AWSConstants.PROPERTY_AUTH_TAG;
import static org.jclouds.aws.reference.AWSConstants.PROPERTY_HEADER_TAG;
import static org.jclouds.crypto.Macs.asByteProcessor;
import static org.jclouds.http.utils.Queries.queryParser;
import static org.jclouds.s3.reference.S3Constants.PROPERTY_S3_SERVICE_PATH;
import static org.jclouds.s3.reference.S3Constants.PROPERTY_S3_VIRTUAL_HOST_BUCKETS;
@ -40,14 +44,12 @@ import javax.ws.rs.core.HttpHeaders;
import org.jclouds.Constants;
import org.jclouds.crypto.Crypto;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.date.TimeStamp;
import org.jclouds.http.HttpException;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpRequestFilter;
import org.jclouds.http.HttpUtils;
import org.jclouds.http.internal.SignatureWire;
import org.jclouds.io.InputSuppliers;
import org.jclouds.logging.Logger;
import org.jclouds.rest.RequestSigner;
import org.jclouds.rest.annotations.Credential;
@ -62,6 +64,7 @@ import com.google.common.collect.Multimap;
import com.google.common.collect.Ordering;
import com.google.common.collect.SortedSetMultimap;
import com.google.common.collect.TreeMultimap;
import com.google.common.io.ByteProcessor;
/**
* Signs the S3 request.
@ -164,14 +167,12 @@ public class RequestAuthorizeSignature implements HttpRequestFilter, RequestSign
}
public String sign(String toSign) {
String signature;
try {
signature = CryptoStreams.base64(CryptoStreams.mac(InputSuppliers.of(toSign), crypto.hmacSHA1(secretKey
.getBytes())));
ByteProcessor<byte[]> hmacSHA1 = asByteProcessor(crypto.hmacSHA1(secretKey.getBytes(UTF_8)));
return base64().encode(readBytes(toInputStream(toSign), hmacSHA1));
} catch (Exception e) {
throw new HttpException("error signing request", e);
}
return signature;
}
void appendMethod(HttpRequest request, StringBuilder toSign) {

View File

@ -18,6 +18,7 @@
*/
package org.jclouds.s3.functions;
import static com.google.common.io.BaseEncoding.base16;
import static org.jclouds.blobstore.reference.BlobStoreConstants.PROPERTY_USER_METADATA_PREFIX;
import java.util.regex.Matcher;
@ -29,7 +30,6 @@ import javax.ws.rs.core.HttpHeaders;
import org.jclouds.blobstore.domain.BlobMetadata;
import org.jclouds.blobstore.functions.ParseSystemAndUserMetadataFromHeaders;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.rest.InvocationContext;
@ -76,7 +76,7 @@ public class ParseObjectMetadataFromHeaders implements Function<HttpResponse, Mu
if (to.getContentMetadata().getContentMD5() == null && to.getETag() != null) {
Matcher md5Matcher = MD5_FROM_ETAG.matcher(to.getETag());
if (md5Matcher.find()) {
byte[] md5 = CryptoStreams.hex(md5Matcher.group(1));
byte[] md5 = base16().lowerCase().decode(md5Matcher.group(1));
// it is possible others will look at the http payload directly
if (from.getPayload() != null)
from.getPayload().getContentMetadata().setContentMD5(md5);

View File

@ -18,12 +18,12 @@
*/
package org.jclouds.s3.xml;
import static com.google.common.io.BaseEncoding.base16;
import static org.jclouds.http.Uris.uriBuilder;
import static org.jclouds.util.SaxUtils.currentOrNull;
import javax.inject.Inject;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.date.DateService;
import org.jclouds.http.functions.ParseSax;
import org.jclouds.s3.domain.CanonicalUser;
@ -99,7 +99,7 @@ public class ListBucketHandler extends ParseSax.HandlerWithResult<ListBucketResp
} else if (qName.equals("ETag")) {
String currentETag = currentOrNull(currentText);
builder.eTag(currentETag);
builder.contentMD5(CryptoStreams.hex(Strings2.replaceAll(currentETag, '"', "")));
builder.contentMD5(base16().lowerCase().decode(Strings2.replaceAll(currentETag, '"', "")));
} else if (qName.equals("Size")) {
builder.contentLength(Long.valueOf(currentOrNull(currentText)));
} else if (qName.equals("Owner")) {

View File

@ -18,6 +18,7 @@
*/
package org.jclouds.s3.functions;
import static com.google.common.io.BaseEncoding.base16;
import static org.easymock.EasyMock.createMock;
import static org.jclouds.aws.reference.AWSConstants.PROPERTY_HEADER_TAG;
import static org.jclouds.blobstore.reference.BlobStoreConstants.PROPERTY_USER_METADATA_PREFIX;
@ -29,7 +30,6 @@ import java.util.Map;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.date.internal.SimpleDateFormatDateService;
import org.jclouds.http.HttpResponse;
import org.jclouds.rest.RequestSigner;
@ -71,7 +71,7 @@ public class ParseObjectMetadataFromHeadersTest {
expects.getContentMetadata().setContentEncoding("encoding");
expects.getContentMetadata().setContentType(MediaType.APPLICATION_OCTET_STREAM);
expects.getContentMetadata().setContentLength(1025l);
expects.getContentMetadata().setContentMD5(CryptoStreams.hex("abcd"));
expects.getContentMetadata().setContentMD5(base16().lowerCase().decode("abcd"));
expects.setETag("\"abcd\"");
expects.setKey("key");
expects.setLastModified(now);
@ -129,7 +129,7 @@ public class ParseObjectMetadataFromHeadersTest {
expects.setCacheControl("cacheControl");
expects.getContentMetadata().setContentDisposition("contentDisposition");
expects.getContentMetadata().setContentEncoding("encoding");
expects.getContentMetadata().setContentMD5(CryptoStreams.hex("abcd"));
expects.getContentMetadata().setContentMD5(base16().lowerCase().decode("abcd"));
expects.getContentMetadata().setContentType(MediaType.APPLICATION_OCTET_STREAM);
expects.getContentMetadata().setContentLength(1025l);
expects.setETag("\"abcd\"");

View File

@ -18,13 +18,13 @@
*/
package org.jclouds.s3.xml;
import static com.google.common.io.BaseEncoding.base16;
import static org.testng.Assert.assertEquals;
import java.io.InputStream;
import java.net.URI;
import java.util.TreeSet;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.date.DateService;
import org.jclouds.date.internal.SimpleDateFormatDateService;
import org.jclouds.http.HttpException;
@ -71,43 +71,43 @@ public class ListBucketHandlerTest extends BaseHandlerTest {
new ObjectMetadataBuilder().key("apps/0").bucket(bucket).uri(URI.create("http://bucket.com/apps/0"))
.lastModified(dateService.iso8601DateParse("2009-05-07T18:27:08.000Z")).eTag(
"\"c82e6a0025c31c5de5947fda62ac51ab\"").owner(owner).contentMD5(
CryptoStreams.hex("c82e6a0025c31c5de5947fda62ac51ab")).contentLength(8l).build(),
base16().lowerCase().decode("c82e6a0025c31c5de5947fda62ac51ab")).contentLength(8l).build(),
new ObjectMetadataBuilder().key("apps/1").bucket(bucket).uri(URI.create("http://bucket.com/apps/1"))
.lastModified(dateService.iso8601DateParse("2009-05-07T18:27:09.000Z")).eTag(
"\"944fab2c5a9a6bacf07db5e688310d7a\"").owner(owner).contentMD5(
CryptoStreams.hex("944fab2c5a9a6bacf07db5e688310d7a")).contentLength(8l).build(),
base16().lowerCase().decode("944fab2c5a9a6bacf07db5e688310d7a")).contentLength(8l).build(),
new ObjectMetadataBuilder().key("apps/2").bucket(bucket).uri(URI.create("http://bucket.com/apps/2"))
.lastModified(dateService.iso8601DateParse("2009-05-07T18:27:09.000Z")).eTag(
"\"a227b8888045c8fd159fb495214000f0\"").owner(owner).contentMD5(
CryptoStreams.hex("a227b8888045c8fd159fb495214000f0")).contentLength(8l).build(),
base16().lowerCase().decode("a227b8888045c8fd159fb495214000f0")).contentLength(8l).build(),
new ObjectMetadataBuilder().key("apps/3").bucket(bucket).uri(URI.create("http://bucket.com/apps/3"))
.lastModified(dateService.iso8601DateParse("2009-05-07T18:27:09.000Z")).eTag(
"\"c9caa76c3dec53e2a192608ce73eef03\"").owner(owner).contentMD5(
CryptoStreams.hex("c9caa76c3dec53e2a192608ce73eef03")).contentLength(8l).build(),
base16().lowerCase().decode("c9caa76c3dec53e2a192608ce73eef03")).contentLength(8l).build(),
new ObjectMetadataBuilder().key("apps/4").bucket(bucket).uri(URI.create("http://bucket.com/apps/4"))
.lastModified(dateService.iso8601DateParse("2009-05-07T18:27:09.000Z")).eTag(
"\"1ce5d0dcc6154a647ea90c7bdf82a224\"").owner(owner).contentMD5(
CryptoStreams.hex("1ce5d0dcc6154a647ea90c7bdf82a224")).contentLength(8l).build(),
base16().lowerCase().decode("1ce5d0dcc6154a647ea90c7bdf82a224")).contentLength(8l).build(),
new ObjectMetadataBuilder().key("apps/5").bucket(bucket).uri(URI.create("http://bucket.com/apps/5"))
.lastModified(dateService.iso8601DateParse("2009-05-07T18:27:09.000Z")).eTag(
"\"79433524d87462ee05708a8ef894ed55\"").owner(owner).contentMD5(
CryptoStreams.hex("79433524d87462ee05708a8ef894ed55")).contentLength(8l).build(),
base16().lowerCase().decode("79433524d87462ee05708a8ef894ed55")).contentLength(8l).build(),
new ObjectMetadataBuilder().key("apps/6").bucket(bucket).uri(URI.create("http://bucket.com/apps/6"))
.lastModified(dateService.iso8601DateParse("2009-05-07T18:27:10.000Z")).eTag(
"\"dd00a060b28ddca8bc5a21a49e306f67\"").owner(owner).contentMD5(
CryptoStreams.hex("dd00a060b28ddca8bc5a21a49e306f67")).contentLength(8l).build(),
base16().lowerCase().decode("dd00a060b28ddca8bc5a21a49e306f67")).contentLength(8l).build(),
new ObjectMetadataBuilder().key("apps/7").bucket(bucket).uri(URI.create("http://bucket.com/apps/7"))
.lastModified(dateService.iso8601DateParse("2009-05-07T18:27:10.000Z")).eTag(
"\"8cd06eca6e819a927b07a285d750b100\"").owner(owner).contentMD5(
CryptoStreams.hex("8cd06eca6e819a927b07a285d750b100")).contentLength(8l).build(),
base16().lowerCase().decode("8cd06eca6e819a927b07a285d750b100")).contentLength(8l).build(),
new ObjectMetadataBuilder().key("apps/8").bucket(bucket).uri(URI.create("http://bucket.com/apps/8"))
.lastModified(dateService.iso8601DateParse("2009-05-07T18:27:10.000Z")).eTag(
"\"174495094d0633b92cbe46603eee6bad\"").owner(owner).contentMD5(
CryptoStreams.hex("174495094d0633b92cbe46603eee6bad")).contentLength(8l).build(),
base16().lowerCase().decode("174495094d0633b92cbe46603eee6bad")).contentLength(8l).build(),
new ObjectMetadataBuilder().key("apps/9").bucket(bucket).uri(URI.create("http://bucket.com/apps/9"))
.lastModified(dateService.iso8601DateParse("2009-05-07T18:27:10.000Z")).eTag(
"\"cd8a19b26fea8a827276df0ad11c580d\"").owner(owner).contentMD5(
CryptoStreams.hex("cd8a19b26fea8a827276df0ad11c580d")).contentLength(8l).build()),
base16().lowerCase().decode("cd8a19b26fea8a827276df0ad11c580d")).contentLength(8l).build()),
"apps/", null, null, 1000, null, false, new TreeSet<String>());
return expected;
}

View File

@ -18,9 +18,9 @@
*/
package org.jclouds.sqs.xml;
import static com.google.common.io.BaseEncoding.base16;
import static org.jclouds.util.SaxUtils.currentOrNull;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.http.functions.ParseSax;
import org.jclouds.sqs.domain.Message;
@ -57,7 +57,7 @@ public class MessageHandler extends ParseSax.HandlerForGeneratedRequestWithResul
} else if (qName.equals("ReceiptHandle")) {
builder.receiptHandle(currentOrNull(currentText));
} else if (qName.equals("MD5OfBody")) {
builder.md5(HashCodes.fromBytes(CryptoStreams.hex(currentOrNull(currentText))));
builder.md5(HashCodes.fromBytes(base16().lowerCase().decode(currentOrNull(currentText))));
} else if (qName.equals("Body")) {
builder.body(currentOrNull(currentText));
} else if (qName.equals("Name")) {

View File

@ -18,12 +18,13 @@
*/
package org.jclouds.sqs.xml;
import static com.google.common.io.BaseEncoding.base16;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.inject.Inject;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.functions.ReturnStringIf2xx;
import org.jclouds.sqs.domain.MessageIdAndMD5;
@ -56,7 +57,7 @@ public class RegexMessageIdAndMD5Handler implements Function<HttpResponse, Messa
if (matcher.find()) {
return MessageIdAndMD5.builder()
.id(matcher.group(1))
.md5(HashCodes.fromBytes(CryptoStreams.hex(matcher.group(2))))
.md5(HashCodes.fromBytes(base16().lowerCase().decode(matcher.group(2))))
.build();
}
}

View File

@ -18,12 +18,12 @@
*/
package org.jclouds.sqs.xml;
import static com.google.common.io.BaseEncoding.base16;
import static org.jclouds.util.SaxUtils.currentOrNull;
import java.util.Map;
import java.util.Map.Entry;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.http.functions.ParseSax;
import org.jclouds.sqs.domain.MessageIdAndMD5;
@ -61,7 +61,7 @@ public class SendMessageBatchResultEntryHandler extends
} else if (qName.equals("MessageId")) {
builder.id(currentOrNull(currentText));
} else if (qName.equals("MD5OfMessageBody")) {
builder.md5(HashCodes.fromBytes(CryptoStreams.hex(currentOrNull(currentText))));
builder.md5(HashCodes.fromBytes(base16().lowerCase().decode(currentOrNull(currentText))));
}
currentText = new StringBuilder();
}

View File

@ -18,11 +18,11 @@
*/
package org.jclouds.sqs.parse;
import static com.google.common.io.BaseEncoding.base16;
import static org.testng.Assert.assertEquals;
import java.io.InputStream;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.http.functions.BaseHandlerTest;
import org.jclouds.sqs.domain.Message;
import org.jclouds.sqs.xml.ReceiveMessageResponseHandler;
@ -58,7 +58,7 @@ public class ReceiveMessageResponseTest extends BaseHandlerTest {
.id("5fea7756-0ea4-451a-a703-a558b933e274")
.receiptHandle(
"+eXJYhj5rDr9cAe/9BuheT5fysi9BoqtEZSkO7IazVbNHg60eCCINxLqaSVv2pFHrWeWNpZwbleSkWRbCtZaQGgpOx/3cWJZiNSG1KKlJX4IOwISFvb3FwByMx4w0lnINeXzcw2VcKQXNrCatO9gdIiVPvJC3SCKatYM/7YTidtjqc8igrtYW2E2mHlCy3NXPCeXxP4tSvyEwIxpDAmMT7IF0mWvTHS6+JBUtFUsrmi61oIHlESNrD1OjdB1QQw+kdvJ6VbsntbJNNYKw+YqdqWNpZkiGQ8y1z9OdHsr1+4=")
.md5(HashCodes.fromBytes(CryptoStreams.hex("fafb00f5732ab283681e124bf8747ed1")))
.md5(HashCodes.fromBytes(base16().lowerCase().decode("fafb00f5732ab283681e124bf8747ed1")))
.body("This is a test message")
.addAttribute("SenderId", "195004372649")
.addAttribute("SentTimestamp", "1238099229000")

View File

@ -18,11 +18,11 @@
*/
package org.jclouds.sqs.parse;
import static com.google.common.io.BaseEncoding.base16;
import static org.testng.Assert.assertEquals;
import java.io.InputStream;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.http.functions.BaseHandlerTest;
import org.jclouds.sqs.domain.BatchResult;
import org.jclouds.sqs.domain.MessageIdAndMD5;
@ -56,10 +56,10 @@ public class SendMessageBatchResponseTest extends BaseHandlerTest {
.<MessageIdAndMD5> builder()
.put("test_msg_001",
MessageIdAndMD5.builder().id("0a5231c7-8bff-4955-be2e-8dc7c50a25fa")
.md5(HashCodes.fromBytes(CryptoStreams.hex("0e024d309850c78cba5eabbeff7cae71"))).build())
.md5(HashCodes.fromBytes(base16().lowerCase().decode("0e024d309850c78cba5eabbeff7cae71"))).build())
.put("test_msg_002",
MessageIdAndMD5.builder().id("15ee1ed3-87e7-40c1-bdaa-2e49968ea7e9")
.md5(HashCodes.fromBytes(CryptoStreams.hex("7fb8146a82f95e0af155278f406862c2"))).build())
.md5(HashCodes.fromBytes(base16().lowerCase().decode("7fb8146a82f95e0af155278f406862c2"))).build())
.build();
}
}

View File

@ -18,11 +18,11 @@
*/
package org.jclouds.sqs.parse;
import static com.google.common.io.BaseEncoding.base16;
import static org.testng.Assert.assertEquals;
import java.io.InputStream;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.functions.ReturnStringIf2xx;
import org.jclouds.sqs.domain.MessageIdAndMD5;
@ -53,6 +53,6 @@ public class SendMessageResponseTest {
public MessageIdAndMD5 expected() {
return MessageIdAndMD5.builder().id("c332b2b0-b61f-42d3-8832-d03ebd89f68d")
.md5(HashCodes.fromBytes(CryptoStreams.hex("e32aedf2b2b25355d04b1507055532e6"))).build();
.md5(HashCodes.fromBytes(base16().lowerCase().decode("e32aedf2b2b25355d04b1507055532e6"))).build();
}
}

View File

@ -18,10 +18,16 @@
*/
package org.jclouds.openstack.swift.blobstore;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.io.BaseEncoding.base16;
import static com.google.common.io.ByteStreams.readBytes;
import static org.jclouds.blobstore.util.BlobStoreUtils.cleanRequest;
import static org.jclouds.crypto.Macs.asByteProcessor;
import static org.jclouds.util.Strings2.toInputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.security.InvalidKeyException;
@ -32,7 +38,6 @@ import org.jclouds.blobstore.BlobRequestSigner;
import org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.functions.BlobToHttpGetOptions;
import org.jclouds.crypto.Crypto;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.date.TimeStamp;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpRequestFilter;
@ -46,6 +51,7 @@ import org.jclouds.rest.internal.RestAnnotationProcessor;
import com.google.common.base.Supplier;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.ByteProcessor;
import com.google.inject.Provider;
/**
@ -146,11 +152,14 @@ public class SwiftBlobSigner<T extends CommonSwiftAsyncClient> implements BlobRe
return String.format("%s\n%d\n%s", method.toUpperCase(), expiresInSeconds, request.getEndpoint().getPath());
}
private String createSignature(String key, String stringToSign) {
private String createSignature(String key, String toSign) {
try {
return CryptoStreams.hex(crypto.hmacSHA1(key.getBytes()).doFinal(stringToSign.getBytes()));
ByteProcessor<byte[]> hmacSHA1 = asByteProcessor(crypto.hmacSHA1(key.getBytes(UTF_8)));
return base16().lowerCase().encode(readBytes(toInputStream(toSign), hmacSHA1));
} catch (InvalidKeyException e) {
throw Throwables.propagate(e);
} catch (IOException e) {
throw Throwables.propagate(e);
}
}
}

View File

@ -18,6 +18,8 @@
*/
package org.jclouds.openstack.swift.blobstore.functions;
import static com.google.common.io.BaseEncoding.base16;
import javax.inject.Inject;
import javax.inject.Singleton;
@ -25,7 +27,6 @@ import org.jclouds.blobstore.domain.MutableBlobMetadata;
import org.jclouds.blobstore.domain.StorageType;
import org.jclouds.blobstore.domain.internal.MutableBlobMetadataImpl;
import org.jclouds.blobstore.strategy.IfDirectoryReturnNameStrategy;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.openstack.swift.domain.MutableObjectInfoWithMetadata;
import org.jclouds.openstack.swift.domain.ObjectInfo;
@ -51,7 +52,7 @@ public class ObjectToBlobMetadata implements Function<ObjectInfo, MutableBlobMet
if (from.getContentType() != null)
to.getContentMetadata().setContentType(from.getContentType());
if (from.getHash() != null)
to.setETag(CryptoStreams.hex(from.getHash()));
to.setETag(base16().lowerCase().encode(from.getHash()));
to.setName(from.getName());
to.setContainer(from.getContainer());
to.setUri(from.getUri());

View File

@ -18,6 +18,8 @@
*/
package org.jclouds.openstack.swift.blobstore.functions;
import static com.google.common.io.BaseEncoding.base16;
import java.util.Map.Entry;
import javax.inject.Singleton;
@ -25,7 +27,6 @@ import javax.inject.Singleton;
import org.jclouds.blobstore.domain.BlobMetadata;
import org.jclouds.blobstore.domain.StorageMetadata;
import org.jclouds.blobstore.domain.StorageType;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.openstack.swift.domain.MutableObjectInfoWithMetadata;
import org.jclouds.openstack.swift.domain.internal.MutableObjectInfoWithMetadataImpl;
@ -49,7 +50,7 @@ public class ResourceToObjectInfo implements Function<StorageMetadata, MutableOb
to.setContentType("application/directory");
}
if (from.getETag() != null && to.getHash() == null)
to.setHash(CryptoStreams.hex(from.getETag()));
to.setHash(base16().lowerCase().decode(from.getETag()));
to.setName(from.getName());
to.setLastModified(from.getLastModified());
if (from.getUserMetadata() != null) {

View File

@ -19,13 +19,13 @@
package org.jclouds.openstack.swift.functions;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.io.BaseEncoding.base16;
import static org.jclouds.http.HttpUtils.attemptToParseSizeAndRangeFromHeaders;
import javax.inject.Inject;
import org.jclouds.blobstore.domain.BlobMetadata;
import org.jclouds.blobstore.functions.ParseSystemAndUserMetadataFromHeaders;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.openstack.swift.blobstore.functions.ResourceToObjectInfo;
@ -64,7 +64,7 @@ public class ParseObjectInfoFromHeaders implements Function<HttpResponse, Mutabl
to.setUri(base.getUri());
String eTagHeader = from.getFirstHeaderOrNull("Etag");
if (eTagHeader != null) {
to.setHash(CryptoStreams.hex(eTagHeader));
to.setHash(base16().lowerCase().decode(eTagHeader));
}
return to;
}

View File

@ -18,6 +18,7 @@
*/
package org.jclouds.openstack.swift;
import static com.google.common.io.BaseEncoding.base16;
import static org.jclouds.openstack.swift.options.ListContainerOptions.Builder.underPath;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
@ -35,7 +36,6 @@ import java.util.concurrent.TimeoutException;
import org.jclouds.blobstore.domain.PageSet;
import org.jclouds.blobstore.integration.internal.BaseBlobStoreIntegrationTest;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.http.HttpResponseException;
import org.jclouds.http.options.GetOptions;
import org.jclouds.io.Payloads;
@ -197,7 +197,8 @@ public abstract class CommonSwiftClientLiveTest<C extends CommonSwiftClient> ext
byte[] md5 = object.getPayload().getContentMetadata().getContentMD5();
String newEtag = getApi().putObject(containerName, object);
assert newEtag != null;
assertEquals(CryptoStreams.hex(md5), CryptoStreams.hex(object.getPayload().getContentMetadata()
assertEquals(base16().lowerCase().encode(md5), base16().lowerCase().encode(object.getPayload().getContentMetadata()
.getContentMD5()));
// Test HEAD of missing object
@ -210,8 +211,8 @@ public abstract class CommonSwiftClientLiveTest<C extends CommonSwiftClient> ext
assertEquals(metadata.getBytes(), Long.valueOf(data.length()));
assert metadata.getContentType().startsWith("text/plain") : metadata.getContentType();
assertEquals(CryptoStreams.hex(md5), CryptoStreams.hex(metadata.getHash()));
assertEquals(metadata.getHash(), CryptoStreams.hex(newEtag));
assertEquals(base16().lowerCase().encode(md5), base16().lowerCase().encode(metadata.getHash()));
assertEquals(metadata.getHash(), base16().lowerCase().decode(newEtag));
assertEquals(metadata.getMetadata().entrySet().size(), 1);
assertEquals(metadata.getMetadata().get("metadata"), "metadata-value");
@ -230,8 +231,8 @@ public abstract class CommonSwiftClientLiveTest<C extends CommonSwiftClient> ext
// object.getMetadata().getName());
assertEquals(getBlob.getInfo().getBytes(), Long.valueOf(data.length()));
testGetObjectContentType(getBlob);
assertEquals(CryptoStreams.hex(md5), CryptoStreams.hex(getBlob.getInfo().getHash()));
assertEquals(CryptoStreams.hex(newEtag), getBlob.getInfo().getHash());
assertEquals(base16().lowerCase().encode(md5), base16().lowerCase().encode(getBlob.getInfo().getHash()));
assertEquals(base16().lowerCase().decode(newEtag), getBlob.getInfo().getHash());
assertEquals(getBlob.getInfo().getMetadata().entrySet().size(), 2);
assertEquals(getBlob.getInfo().getMetadata().get("new-metadata-1"), "value-1");
assertEquals(getBlob.getInfo().getMetadata().get("new-metadata-2"), "value-2");
@ -240,7 +241,7 @@ public abstract class CommonSwiftClientLiveTest<C extends CommonSwiftClient> ext
// transit)
String correctEtag = newEtag;
String incorrectEtag = "0" + correctEtag.substring(1);
object.getInfo().setHash(CryptoStreams.hex(incorrectEtag));
object.getInfo().setHash(base16().lowerCase().decode(incorrectEtag));
try {
getApi().putObject(containerName, object);
} catch (HttpResponseException e) {
@ -253,7 +254,7 @@ public abstract class CommonSwiftClientLiveTest<C extends CommonSwiftClient> ext
blob.getInfo().setName("chunked-object");
blob.setPayload(bais);
newEtag = getApi().putObject(containerName, blob);
assertEquals(CryptoStreams.hex(md5), CryptoStreams.hex(getBlob.getInfo().getHash()));
assertEquals(base16().lowerCase().encode(md5), base16().lowerCase().encode(getBlob.getInfo().getHash()));
// Test GET with options
// Non-matching ETag
@ -268,7 +269,7 @@ public abstract class CommonSwiftClientLiveTest<C extends CommonSwiftClient> ext
// Matching ETag
getBlob = getApi().getObject(containerName, object.getInfo().getName(),
GetOptions.Builder.ifETagMatches(newEtag));
assertEquals(getBlob.getInfo().getHash(), CryptoStreams.hex(newEtag));
assertEquals(getBlob.getInfo().getHash(), base16().lowerCase().decode(newEtag));
getBlob = getApi().getObject(containerName, object.getInfo().getName(), GetOptions.Builder.startAt(8));
assertEquals(Strings2.toString(getBlob.getPayload()), data.substring(8));

View File

@ -18,23 +18,23 @@
*/
package org.jclouds.openstack.swift.blobstore.integration;
import com.google.common.io.Files;
import com.google.common.io.InputSupplier;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.jclouds.blobstore.BlobStore;
import org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.integration.internal.BaseBlobIntegrationTest;
import org.jclouds.blobstore.options.PutOptions;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.openstack.keystone.v2_0.config.KeystoneProperties;
import org.testng.ITestContext;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.util.Properties;
import com.google.common.io.Files;
import com.google.common.io.InputSupplier;
/**
*
@ -68,7 +68,7 @@ public class SwiftBlobIntegrationLiveTest extends BaseBlobIntegrationTest {
public void setUpResourcesOnThisThread(ITestContext testContext) throws Exception {
super.setUpResourcesOnThisThread(testContext);
oneHundredOneConstitutions = getTestDataSupplier();
oneHundredOneConstitutionsMD5 = CryptoStreams.md5(oneHundredOneConstitutions);
oneHundredOneConstitutionsMD5 = md5Supplier(oneHundredOneConstitutions);
}
@Override

View File

@ -18,6 +18,7 @@
*/
package org.jclouds.openstack.swift.domain.internal;
import static com.google.common.io.BaseEncoding.base16;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
@ -27,7 +28,6 @@ import java.io.InputStream;
import java.net.URI;
import java.util.Set;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.date.internal.SimpleDateFormatDateService;
import org.jclouds.json.config.GsonModule;
import org.jclouds.json.config.GsonModule.DateAdapter;
@ -65,12 +65,12 @@ public class ParseObjectInfoListFromJsonResponseTest {
InputStream is = getClass().getResourceAsStream("/test_list_container.json");
Set<ObjectInfo> expects = ImmutableSet.<ObjectInfo> of(ObjectInfoImpl.builder().container("container").name(
"test_obj_1").uri(URI.create("http://localhost/foo/test_obj_1")).hash(
CryptoStreams.hex("4281c348eaf83e70ddce0e07221c3d28")).bytes(14l)
base16().lowerCase().decode("4281c348eaf83e70ddce0e07221c3d28")).bytes(14l)
.contentType("application/octet-stream").lastModified(
new SimpleDateFormatDateService().iso8601DateParse("2009-02-03T05:26:32.612Z")).build(),
ObjectInfoImpl.builder().container("container").name("test_obj_2").uri(
URI.create("http://localhost/foo/test_obj_2")).hash(
CryptoStreams.hex("b039efe731ad111bc1b0ef221c3849d0")).bytes(64l).contentType(
base16().lowerCase().decode("b039efe731ad111bc1b0ef221c3849d0")).bytes(64l).contentType(
"application/octet-stream").lastModified(
new SimpleDateFormatDateService().iso8601DateParse("2009-02-03T05:26:32.612Z")).build());
GeneratedHttpRequest request = createMock(GeneratedHttpRequest.class);

View File

@ -18,10 +18,11 @@
*/
package org.jclouds.vcloud.features;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.collect.Iterables.get;
import static com.google.common.collect.Iterables.getOnlyElement;
import static com.google.common.io.BaseEncoding.base64;
import static org.jclouds.compute.options.RunScriptOptions.Builder.wrapInInitScript;
import static org.jclouds.crypto.CryptoStreams.base64;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
@ -119,7 +120,7 @@ public class VmClientLiveTest extends BaseVCloudClientLiveTest {
ExecResponse vmTools = client.runScriptOnNode(node.getId(), PARSE_VMTOOLSD,
wrapInInitScript(false).runAsRoot(false));
checkApiOutput(new String(base64(vmTools.getOutput().trim())));
checkApiOutput(new String(base64().decode(vmTools.getOutput().trim()), UTF_8));
ExecResponse foo = client.runScriptOnNode(node.getId(), "cat /root/foo.txt", wrapInInitScript(false)
.runAsRoot(false));

View File

@ -54,20 +54,7 @@ See http://code.google.com/p/jclouds for details."
[org.jclouds.io Payload Payloads payloads.StreamingPayload]
java.util.Arrays
[java.security DigestOutputStream MessageDigest]
com.google.common.collect.ImmutableSet
org.jclouds.encryption.internal.JCECrypto))
(def ^{:private true}
crypto-impl
;; BouncyCastle might not be present. Try to load it, but fall back to
;; JCECrypto if we can't.
(try
(import 'org.jclouds.encryption.bouncycastle.BouncyCastleCrypto)
(.newInstance
(Class/forName
"org.jclouds.encryption.bouncycastle.BouncyCastleCrypto"))
(catch Exception e
(JCECrypto.))))
com.google.common.collect.ImmutableSet))
;;
;; Payload support for creating Blobs.
@ -314,7 +301,7 @@ Options can also be specified for extension modules
content-disposition content-encoding content-language metadata]}]
{:pre [(not (and content-md5 calculate-md5))
(not (and (nil? payload) calculate-md5))]}
(let [blob-builder (.name (BlobBuilderImpl. crypto-impl) name)
(let [blob-builder (.name (BlobBuilderImpl.) name)
blob-builder (if payload
(.payload blob-builder
(org.jclouds.blobstore2/payload payload))

View File

@ -19,6 +19,8 @@
package org.jclouds.blobstore;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.io.BaseEncoding.base16;
import static org.jclouds.http.Uris.uriBuilder;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@ -34,12 +36,9 @@ import org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.domain.Blob.Factory;
import org.jclouds.blobstore.options.ListContainerOptions;
import org.jclouds.blobstore.util.BlobStoreUtils;
import org.jclouds.crypto.Crypto;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.date.DateService;
import org.jclouds.domain.Location;
import org.jclouds.http.HttpUtils;
import static org.jclouds.http.Uris.*;
import org.jclouds.io.ContentMetadataCodec;
import org.jclouds.io.MutableContentMetadata;
import org.jclouds.io.Payload;
@ -58,16 +57,14 @@ public class TransientStorageStrategy implements LocalStorageStrategy {
private final Supplier<Location> defaultLocation;
private final DateService dateService;
private final Factory blobFactory;
private final Crypto crypto;
private final ContentMetadataCodec contentMetadataCodec;
@Inject
TransientStorageStrategy(Supplier<Location> defaultLocation, DateService dateService, Factory blobFactory,
Crypto crypto, ContentMetadataCodec contentMetadataCodec) {
ContentMetadataCodec contentMetadataCodec) {
this.defaultLocation = defaultLocation;
this.dateService = dateService;
this.blobFactory = blobFactory;
this.crypto = crypto;
this.contentMetadataCodec = contentMetadataCodec;
}
@ -130,9 +127,8 @@ public class TransientStorageStrategy implements LocalStorageStrategy {
Blob newBlob = createUpdatedCopyOfBlobInContainer(containerName, blob);
Map<String, Blob> map = containerToBlobs.get(containerName);
map.put(newBlob.getMetadata().getName(), newBlob);
Payloads.calculateMD5(newBlob, crypto.md5());
String eTag = CryptoStreams.hex(newBlob.getPayload().getContentMetadata().getContentMD5());
return eTag;
Payloads.calculateMD5(newBlob);
return base16().lowerCase().encode(newBlob.getPayload().getContentMetadata().getContentMD5());
}
@Override
@ -170,7 +166,7 @@ public class TransientStorageStrategy implements LocalStorageStrategy {
HttpUtils.copy(oldMd, payload.getContentMetadata());
} else {
if (payload.getContentMetadata().getContentMD5() == null)
Payloads.calculateMD5(in, crypto.md5());
Payloads.calculateMD5(in);
}
} catch (IOException e) {
Throwables.propagate(e);
@ -181,7 +177,7 @@ public class TransientStorageStrategy implements LocalStorageStrategy {
blob.getMetadata().setUri(
uriBuilder(new StringBuilder("mem://").append(containerName)).path(in.getMetadata().getName()).build());
blob.getMetadata().setLastModified(new Date());
String eTag = CryptoStreams.hex(payload.getContentMetadata().getContentMD5());
String eTag = base16().lowerCase().encode(payload.getContentMetadata().getContentMD5());
blob.getMetadata().setETag(eTag);
// Set HTTP headers to match metadata
blob.getAllHeaders().replaceValues(HttpHeaders.LAST_MODIFIED,

View File

@ -32,7 +32,6 @@ import org.jclouds.blobstore.strategy.ContainsValueInListStrategy;
import org.jclouds.blobstore.strategy.GetBlobsInListStrategy;
import org.jclouds.blobstore.strategy.PutBlobsStrategy;
import org.jclouds.blobstore.strategy.internal.ListContainerAndRecurseThroughFolders;
import org.jclouds.crypto.Crypto;
import com.google.inject.AbstractModule;
import com.google.inject.Scopes;
@ -87,13 +86,11 @@ public class BlobStoreMapModule extends AbstractModule {
@Inject
PutBlobsStrategy putBlobsStrategy;
@Inject
Crypto crypto;
@Inject
ListContainerAndRecurseThroughFolders listStrategy;
public InputStreamMap create(String containerName, ListContainerOptions options) {
return new InputStreamMapImpl(connection, blobBuilders, getAllBlobs, listStrategy, containsValueStrategy,
putBlobsStrategy, containerName, options, crypto);
putBlobsStrategy, containerName, options);
}
}

View File

@ -27,12 +27,9 @@ import java.io.InputStream;
import java.util.Date;
import java.util.Map;
import javax.inject.Inject;
import org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.domain.BlobBuilder;
import org.jclouds.blobstore.domain.StorageType;
import org.jclouds.crypto.Crypto;
import org.jclouds.io.Payload;
import org.jclouds.io.Payloads;
import org.jclouds.io.payloads.PhantomPayload;
@ -43,12 +40,6 @@ import com.google.common.collect.Maps;
* @author Adrian Cole
*/
public class BlobBuilderImpl implements BlobBuilder {
private final Crypto crypto;
@Inject
public BlobBuilderImpl(Crypto crypto) {
this.crypto = checkNotNull(crypto, "crypto");
}
private Payload payload;
private String name;
@ -77,7 +68,7 @@ public class BlobBuilderImpl implements BlobBuilder {
@Override
public PayloadBlobBuilder payload(Payload payload) {
this.payload = payload;
return new PayloadBlobBuilderImpl(this, payload, crypto);
return new PayloadBlobBuilderImpl(this, payload);
}
/**
@ -127,12 +118,10 @@ public class BlobBuilderImpl implements BlobBuilder {
public class PayloadBlobBuilderImpl implements PayloadBlobBuilder {
private final BlobBuilder builder;
private final Payload payload;
private final Crypto crypto;
public PayloadBlobBuilderImpl(BlobBuilder builder, Payload payload, Crypto crypto) {
public PayloadBlobBuilderImpl(BlobBuilder builder, Payload payload) {
this.builder = checkNotNull(builder, "builder");
this.payload = checkNotNull(payload, "payload");
this.crypto = checkNotNull(crypto, "crypto");
}
@Override
@ -157,7 +146,7 @@ public class BlobBuilderImpl implements BlobBuilder {
@Override
public PayloadBlobBuilder calculateMD5() throws IOException {
return builder.payload(Payloads.calculateMD5(payload, crypto.md5()));
return builder.payload(Payloads.calculateMD5(payload));
}
@Override

View File

@ -22,10 +22,8 @@ import static com.google.common.base.Preconditions.checkNotNull;
import java.io.IOException;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.crypto.Crypto;
import org.jclouds.http.HttpMessage;
import org.jclouds.io.PayloadEnclosing;
import org.jclouds.io.Payloads;
@ -40,13 +38,6 @@ import com.google.common.base.Throwables;
@Singleton
public class ObjectMD5 implements Function<Object, byte[]> {
protected final Crypto crypto;
@Inject
ObjectMD5(Crypto crypto) {
this.crypto = checkNotNull(crypto, "crypto");
}
public byte[] apply(Object from) {
checkNotNull(from, "thing to md5");
PayloadEnclosing payloadEnclosing;
@ -57,7 +48,7 @@ public class ObjectMD5 implements Function<Object, byte[]> {
}
if (payloadEnclosing.getPayload().getContentMetadata().getContentMD5() == null)
try {
Payloads.calculateMD5(payloadEnclosing, crypto.md5());
Payloads.calculateMD5(payloadEnclosing);
} catch (IOException e) {
Throwables.propagate(e);
}

View File

@ -31,6 +31,7 @@ import java.util.Map;
import javax.inject.Inject;
import javax.inject.Provider;
import org.jclouds.blobstore.BlobMap;
import org.jclouds.blobstore.BlobStore;
import org.jclouds.blobstore.InputStreamMap;
import org.jclouds.blobstore.domain.Blob;
@ -40,7 +41,6 @@ import org.jclouds.blobstore.strategy.ContainsValueInListStrategy;
import org.jclouds.blobstore.strategy.GetBlobsInListStrategy;
import org.jclouds.blobstore.strategy.PutBlobsStrategy;
import org.jclouds.blobstore.strategy.internal.ListContainerAndRecurseThroughFolders;
import org.jclouds.crypto.Crypto;
import org.jclouds.io.Payload;
import org.jclouds.io.Payloads;
import org.jclouds.io.payloads.ByteArrayPayload;
@ -63,15 +63,13 @@ import com.google.common.base.Throwables;
* @see BaseBlobMap
*/
public class InputStreamMapImpl extends BaseBlobMap<InputStream> implements InputStreamMap {
protected final Crypto crypto;
@Inject
public InputStreamMapImpl(BlobStore connection, Provider<BlobBuilder> blobBuilders,
GetBlobsInListStrategy getAllBlobs, ListContainerAndRecurseThroughFolders listStrategy,
ContainsValueInListStrategy containsValueStrategy, PutBlobsStrategy putBlobsStrategy, String containerName,
ListContainerOptions options, Crypto crypto) {
ListContainerOptions options) {
super(connection, getAllBlobs, containsValueStrategy, putBlobsStrategy, listStrategy, containerName, options);
this.crypto = crypto;
}
@Override
@ -146,7 +144,7 @@ public class InputStreamMapImpl extends BaseBlobMap<InputStream> implements Inpu
Blob newBlobWithMD5(String name, Object value) {
Blob blob = blobstore.blobBuilder(prefixer.apply(name)).payload(newPayload(value)).build();
try {
Payloads.calculateMD5(blob, crypto.md5());
Payloads.calculateMD5(blob);
} catch (IOException e) {
Throwables.propagate(e);
}

View File

@ -21,7 +21,6 @@
(:use [org.jclouds.blobstore2] :reload-all)
(:use [clojure.test])
(:import [org.jclouds.blobstore BlobStoreContextFactory]
[org.jclouds.crypto CryptoStreams]
[java.io ByteArrayInputStream ByteArrayOutputStream
StringBufferInputStream]
[org.jclouds.util Strings2]))
@ -152,10 +151,12 @@
(deftest blob-test
(let [a-blob (blob "test-name"
:payload (.getBytes "test-payload")
:payload "test-payload"
:calculate-md5 true)]
(is (= (seq (.. a-blob (getPayload) (getContentMetadata) (getContentMD5)))
(seq (CryptoStreams/md5 (.getBytes "test-payload")))))))
(seq (.digest (doto (java.security.MessageDigest/getInstance "MD5")
(.reset)
(.update (.getBytes "test-payload")))))))))
(deftest payload-protocol-test
(is (instance? org.jclouds.io.Payload (payload "test")))

View File

@ -18,14 +18,12 @@
*/
package org.jclouds.blobstore.functions;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.hash.Hashing.md5;
import static org.testng.Assert.assertEquals;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.encryption.internal.JCECrypto;
import org.jclouds.http.HttpMessage;
import org.jclouds.io.Payload;
import org.jclouds.io.Payloads;
@ -37,34 +35,26 @@ import org.testng.annotations.Test;
*/
@Test(groups = "unit")
public class ObjectMD5Test {
private ObjectMD5 fn;
public ObjectMD5Test() throws NoSuchAlgorithmException, CertificateException {
fn = new ObjectMD5(new JCECrypto());
}
private ObjectMD5 fn = new ObjectMD5();
@Test
public void testAlreadyHasMD5() {
Payload payload = Payloads.newPayload("foo");
payload.getContentMetadata().setContentMD5(new byte[] {});
HttpMessage payloadEnclosing = HttpMessage.builder().payload(payload).build();
assertEquals(fn.apply(payloadEnclosing), new byte[] {});
}
@Test
public void testMD5PayloadEnclosing() throws IOException {
Payload payload = Payloads.newPayload("foo");
HttpMessage payloadEnclosing = HttpMessage.builder().payload(payload).build();
assertEquals(fn.apply(payloadEnclosing), CryptoStreams.md5("foo".getBytes()));
assertEquals(fn.apply(payloadEnclosing), md5().hashString("foo", UTF_8).asBytes());
}
@Test
public void testMD5String() throws IOException {
assertEquals(fn.apply("foo"), CryptoStreams.md5("foo".getBytes()));
assertEquals(fn.apply("foo"), md5().hashString("foo", UTF_8).asBytes());
}
@Test(expectedExceptions = { NullPointerException.class, IllegalStateException.class })

View File

@ -18,6 +18,8 @@
*/
package org.jclouds.blobstore.integration.internal;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.hash.Hashing.md5;
import static org.jclouds.blobstore.options.GetOptions.Builder.ifETagDoesntMatch;
import static org.jclouds.blobstore.options.GetOptions.Builder.ifETagMatches;
import static org.jclouds.blobstore.options.GetOptions.Builder.ifModifiedSince;
@ -25,6 +27,7 @@ import static org.jclouds.blobstore.options.GetOptions.Builder.ifUnmodifiedSince
import static org.jclouds.blobstore.options.GetOptions.Builder.range;
import static org.jclouds.blobstore.util.BlobStoreUtils.getContentAsStringOrNullAndClose;
import static org.jclouds.concurrent.FutureIterables.awaitCompletion;
import static org.jclouds.io.ByteSources.asByteSource;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
@ -58,7 +61,6 @@ import org.jclouds.blobstore.domain.StorageMetadata;
import org.jclouds.blobstore.domain.StorageType;
import org.jclouds.concurrent.Futures;
import org.jclouds.crypto.Crypto;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.encryption.internal.JCECrypto;
import org.jclouds.http.BaseJettyTest;
import org.jclouds.http.HttpResponseException;
@ -97,7 +99,11 @@ public class BaseBlobIntegrationTest extends BaseBlobStoreIntegrationTest {
public void setUpResourcesOnThisThread(ITestContext testContext) throws Exception {
super.setUpResourcesOnThisThread(testContext);
oneHundredOneConstitutions = getTestDataSupplier();
oneHundredOneConstitutionsMD5 = CryptoStreams.md5(oneHundredOneConstitutions);
oneHundredOneConstitutionsMD5 = md5Supplier(oneHundredOneConstitutions);
}
protected static byte[] md5Supplier(InputSupplier<InputStream> supplier) throws IOException {
return asByteSource(supplier.getInput()).hash(md5()).asBytes();
}
@SuppressWarnings("unchecked")
@ -128,7 +134,7 @@ public class BaseBlobIntegrationTest extends BaseBlobStoreIntegrationTest {
final Payload testPayload = Payloads.newFilePayload(payloadFile);
final byte[] md5 = CryptoStreams.md5(testPayload);
final byte[] md5 = md5Supplier(testPayload);
testPayload.getContentMetadata().setContentType("image/png");
final AtomicInteger blobCount = new AtomicInteger();
@ -147,7 +153,7 @@ public class BaseBlobIntegrationTest extends BaseBlobStoreIntegrationTest {
assertConsistencyAwareBlobExists(container, name);
blob = view.getBlobStore().getBlob(container, name);
assert Arrays.equals(CryptoStreams.md5(blob.getPayload()), md5) : String.format(
assert Arrays.equals(md5Supplier(blob.getPayload()), md5) : String.format(
"md5 didn't match on %s/%s", container, name);
view.getBlobStore().removeBlob(container, name);
@ -184,7 +190,7 @@ public class BaseBlobIntegrationTest extends BaseBlobStoreIntegrationTest {
public Void apply(Blob from) {
try {
validateMetadata(from.getMetadata(), container, name);
assertEquals(CryptoStreams.md5(from.getPayload()), oneHundredOneConstitutionsMD5);
assertEquals(md5Supplier(from.getPayload()), oneHundredOneConstitutionsMD5);
checkContentDisposition(from, expectedContentDisposition);
} catch (IOException e) {
Throwables.propagate(e);
@ -598,7 +604,7 @@ public class BaseBlobIntegrationTest extends BaseBlobStoreIntegrationTest {
}
protected void checkMD5(BlobMetadata metadata) throws IOException {
assertEquals(metadata.getContentMetadata().getContentMD5(), CryptoStreams.md5(InputSuppliers.of(TEST_STRING)));
assertEquals(metadata.getContentMetadata().getContentMD5(), md5().hashString(TEST_STRING, UTF_8).asBytes());
}
}

View File

@ -19,10 +19,10 @@
package org.jclouds.blobstore.integration.internal;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.io.BaseEncoding.base16;
import static org.testng.Assert.assertEquals;
import org.jclouds.blobstore.domain.Blob;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.testng.annotations.Optional;
@ -53,7 +53,7 @@ public class BaseBlobLiveTest extends BaseBlobStoreIntegrationTest {
long length = response.getPayload().getContentMetadata().getContentLength();
String name = "hello";
byte[] md5 = CryptoStreams.hex(httpStreamMD5);
byte[] md5 = base16().lowerCase().decode(httpStreamMD5);
Blob blob = view.getBlobStore().blobBuilder(name).payload(response.getPayload()).contentLength(length)
.contentMD5(md5).build();

View File

@ -18,8 +18,10 @@
*/
package org.jclouds.blobstore.integration.internal;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.base.Throwables.propagateIfPossible;
import static com.google.common.collect.Iterables.get;
import static com.google.common.hash.Hashing.md5;
import static org.jclouds.blobstore.options.ListContainerOptions.Builder.afterMarker;
import static org.jclouds.blobstore.options.ListContainerOptions.Builder.inDirectory;
import static org.jclouds.blobstore.options.ListContainerOptions.Builder.maxResults;
@ -36,8 +38,6 @@ import org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.domain.BlobMetadata;
import org.jclouds.blobstore.domain.PageSet;
import org.jclouds.blobstore.domain.StorageMetadata;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.io.InputSuppliers;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
@ -97,7 +97,7 @@ public class BaseContainerIntegrationTest extends BaseBlobStoreIntegrationTest {
}
protected void checkMD5(BlobMetadata metadata) throws IOException {
assertEquals(metadata.getContentMetadata().getContentMD5(), CryptoStreams.md5(InputSuppliers.of(TEST_STRING)));
assertEquals(metadata.getContentMetadata().getContentMD5(), md5().hashString(TEST_STRING, UTF_8).asBytes());
}
@Test(groups = { "integration", "live" })

View File

@ -18,9 +18,12 @@
*/
package org.jclouds.aws.filters;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.Ordering.natural;
import static com.google.common.io.BaseEncoding.base64;
import static com.google.common.io.ByteStreams.readBytes;
import static org.jclouds.aws.reference.FormParameters.ACTION;
import static org.jclouds.aws.reference.FormParameters.AWS_ACCESS_KEY_ID;
import static org.jclouds.aws.reference.FormParameters.SIGNATURE;
@ -28,8 +31,7 @@ import static org.jclouds.aws.reference.FormParameters.SIGNATURE_METHOD;
import static org.jclouds.aws.reference.FormParameters.SIGNATURE_VERSION;
import static org.jclouds.aws.reference.FormParameters.TIMESTAMP;
import static org.jclouds.aws.reference.FormParameters.VERSION;
import static org.jclouds.crypto.CryptoStreams.base64;
import static org.jclouds.crypto.CryptoStreams.mac;
import static org.jclouds.crypto.Macs.asByteProcessor;
import static org.jclouds.http.utils.Queries.encodeQueryLine;
import static org.jclouds.http.utils.Queries.queryParser;
import static org.jclouds.util.Strings2.toInputStream;
@ -52,7 +54,6 @@ import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpRequestFilter;
import org.jclouds.http.HttpUtils;
import org.jclouds.http.internal.SignatureWire;
import org.jclouds.io.InputSuppliers;
import org.jclouds.logging.Logger;
import org.jclouds.rest.RequestSigner;
import org.jclouds.rest.annotations.ApiVersion;
@ -64,6 +65,7 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap;
import com.google.common.collect.TreeMultimap;
import com.google.common.io.ByteProcessor;
/**
*
@ -163,10 +165,11 @@ public class FormSigner implements HttpRequestFilter, RequestSigner {
}
@VisibleForTesting
public String sign(String stringToSign) {
public String sign(String toSign) {
String signature;
try {
signature = base64(mac(InputSuppliers.of(stringToSign), crypto.hmacSHA256(secretKey.getBytes())));
ByteProcessor<byte[]> hmacSHA256 = asByteProcessor(crypto.hmacSHA256(secretKey.getBytes(UTF_8)));
signature = base64().encode(readBytes(toInputStream(toSign), hmacSHA256));
if (signatureWire.enabled())
signatureWire.input(toInputStream(signature));
} catch (Exception e) {

View File

@ -18,7 +18,11 @@
*/
package org.jclouds.azure.storage.filters;
import static com.google.common.io.BaseEncoding.base64;
import static com.google.common.io.ByteStreams.readBytes;
import static org.jclouds.crypto.Macs.asByteProcessor;
import static org.jclouds.util.Patterns.NEWLINE_PATTERN;
import static org.jclouds.util.Strings2.toInputStream;
import java.util.Collection;
import java.util.Set;
@ -32,14 +36,12 @@ import javax.ws.rs.core.HttpHeaders;
import org.jclouds.Constants;
import org.jclouds.crypto.Crypto;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.date.TimeStamp;
import org.jclouds.http.HttpException;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpRequestFilter;
import org.jclouds.http.HttpUtils;
import org.jclouds.http.internal.SignatureWire;
import org.jclouds.io.InputSuppliers;
import org.jclouds.logging.Logger;
import org.jclouds.rest.annotations.Credential;
import org.jclouds.rest.annotations.Identity;
@ -52,6 +54,7 @@ import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.collect.Multimaps;
import com.google.common.collect.Sets;
import com.google.common.io.ByteProcessor;
/**
* Signs the Azure Storage request.
@ -83,7 +86,7 @@ public class SharedKeyLiteAuthentication implements HttpRequestFilter {
this.utils = utils;
this.signatureWire = signatureWire;
this.identity = identity;
this.key = CryptoStreams.base64(encodedKey);
this.key = base64().decode(encodedKey);
this.timeStampProvider = timeStampProvider;
}
@ -139,13 +142,12 @@ public class SharedKeyLiteAuthentication implements HttpRequestFilter {
}
public String signString(String toSign) {
String signature;
try {
signature = CryptoStreams.base64(CryptoStreams.mac(InputSuppliers.of(toSign), crypto.hmacSHA256(key)));
ByteProcessor<byte[]> hmacSHA256 = asByteProcessor(crypto.hmacSHA256(key));
return base64().encode(readBytes(toInputStream(toSign), hmacSHA256));
} catch (Exception e) {
throw new HttpException("error signing request", e);
}
return signature;
}
private void appendMethod(HttpRequest request, StringBuilder toSign) {

View File

@ -21,7 +21,6 @@ package org.jclouds.crypto;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.KeyPairGenerator;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateFactory;
@ -34,8 +33,7 @@ import org.jclouds.encryption.internal.JCECrypto;
import com.google.inject.ImplementedBy;
/**
* Allows you to access cryptographic objects and factories without adding a
* provider to the JCE runtime.
* Allows you to access cryptographic objects and factories without adding a provider to the JCE runtime.
*
* @author Adrian Cole
*/
@ -53,16 +51,6 @@ public interface Crypto {
Mac hmacSHA1(byte[] key) throws InvalidKeyException;
MessageDigest digest(String algorithm) throws NoSuchAlgorithmException;
MessageDigest md5();
MessageDigest sha1();
MessageDigest sha256();
MessageDigest sha512();
Cipher cipher(String algorithm) throws NoSuchAlgorithmException, NoSuchPaddingException;
}

View File

@ -1,393 +0,0 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License 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 org.jclouds.crypto;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.crypto.Mac;
import org.jclouds.encoding.internal.FlexBase64;
import org.jclouds.io.InputSuppliers;
import com.google.common.annotations.Beta;
import com.google.common.base.Charsets;
import com.google.common.base.Throwables;
import com.google.common.io.ByteProcessor;
import com.google.common.io.ByteStreams;
import com.google.common.io.InputSupplier;
/**
* functions related to but not in {@link com.google.common.io.ByteStreams}
*
* @author Adrian Cole
*/
@Beta
public class CryptoStreams {
private static char[] hex(byte[] in, int offset, int len) {
char[] hex = new char[2 * len];
int index = 0;
for (int i = offset; i < offset + len; ++i) {
byte b = in[i];
int v = b & 0xFF;
hex[index++] = HEX_CHAR_TABLE[v >>> 4];
hex[index++] = HEX_CHAR_TABLE[v & 0xF];
}
return hex;
}
public static String hex(byte[] in) {
return new String(hex(in, 0, in.length));
}
public static byte[] hex(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
}
return data;
}
public static String base64(byte[] in) {
return new String(FlexBase64.encodeBytes(in, 0, in.length, false), Charsets.US_ASCII);
}
/**
* encodes value substituting {@code '-' and '_'} for {@code '+' and '/'},
* and without adding trailing {@code '='} padding.
*
* <h3>Note</h3>
* This utility will be replaced with Guava 14+ BaseEncoding.base64Url()
*
* @see <a
* href="http://en.wikipedia.org/wiki/Base64#URL_applications">url-safe
* encoding</a>
*/
@Beta
public static String base64Url(byte[] in) {
return FlexBase64.encodeURLString(in, 0, in.length);
}
/**
* decodes base 64 encoded string, regardless of whether padding {@code '='} padding is present.
*
* Note this seamlessly handles the URL-safe case where {@code '-' and '_'} are substituted for {@code '+' and '/'}.
*
* @see <a
* href="http://en.wikipedia.org/wiki/Base64#URL_applications">url-safe
* encoding</a>
*/
public static byte[] base64(String in) {
try {
ByteBuffer buffer = FlexBase64.decode(in);
byte [] returnVal = new byte [buffer.limit()];
System.arraycopy(buffer.array(), buffer.arrayOffset(), returnVal, 0, buffer.limit());
return returnVal;
} catch (IOException e) {
// unlikely as this is not reading from a stream
throw Throwables.propagate(e);
}
}
/**
* @see #md5
* @see #hex
*/
public static String md5Hex(InputSupplier<? extends InputStream> supplier) throws IOException {
return hex(md5(supplier));
}
/**
* @see #md5Hex
*/
public static String md5Hex(final String in) {
try {
return md5Hex(new InputSupplier<InputStream>() {
@Override
public InputStream getInput() throws IOException {
return new ByteArrayInputStream(in.getBytes());
}
});
} catch (IOException e) {
// risk is not here when reading from in.getBytes() so wrapping in runtime
throw Throwables.propagate(e);
}
}
/**
* @see #md5
* @see #base64
*/
public static String md5Base64(InputSupplier<? extends InputStream> supplier) throws IOException {
return base64(md5(supplier));
}
/**
* Computes and returns the MAC value for a supplied input stream. The mac
* object is reset when this method returns successfully.
*
* @param supplier
* the input stream factory
* @param mac
* the mac object
* @return the result of {@link Mac#doFinal()} after updating the mac object
* with all of the bytes in the stream and encoding in Base64
* @throws IOException
* if an I/O error occurs
*/
public static String macBase64(InputSupplier<? extends InputStream> supplier, final Mac mac) throws IOException {
return base64(mac(supplier, mac));
}
/**
* Computes and returns the Digest value for a supplied input stream. The
* digest object is reset when this method returns successfully.
*
* @param supplier
* the input stream factory
* @param md
* the digest object
* @return the result of {@link MessageDigest#digest()} after updating the
* digest object with all of the bytes in the stream
* @throws IOException
* if an I/O error occurs
*/
public static byte[] digest(InputSupplier<? extends InputStream> supplier, final MessageDigest md)
throws IOException {
return com.google.common.io.ByteStreams.readBytes(supplier, new ByteProcessor<byte[]>() {
public boolean processBytes(byte[] buf, int off, int len) {
md.update(buf, off, len);
return true;
}
public byte[] getResult() {
return md.digest();
}
});
}
/**
* Computes and returns the SHA1 value for a supplied input stream. A digest
* object is created and disposed of at runtime, consider using
* {@link #digest} to be more efficient.
*
* @param supplier
* the input stream factory
*
* @return the result of {@link MessageDigest#digest()} after updating the
* sha1 object with all of the bytes in the stream
* @throws IOException
* if an I/O error occurs
*/
public static byte[] sha1(InputSupplier<? extends InputStream> supplier) throws IOException {
try {
return digest(supplier, MessageDigest.getInstance("SHA1"));
} catch (NoSuchAlgorithmException e) {
throw Throwables.propagate(e);
}
}
public static byte[] sha1(byte[] in) {
try {
return sha1(ByteStreams.newInputStreamSupplier(in));
} catch (IOException e) {
throw Throwables.propagate(e);
}
}
/**
* Computes and returns the MD5 value for a supplied input stream. A digest
* object is created and disposed of at runtime, consider using
* {@link #digest} to be more efficient.
*
* @param supplier
* the input stream factory
*
* @return the result of {@link MessageDigest#digest()} after updating the
* md5 object with all of the bytes in the stream
* @throws IOException
* if an I/O error occurs
*/
public static byte[] md5(InputSupplier<? extends InputStream> supplier) throws IOException {
try {
return digest(supplier, MessageDigest.getInstance("MD5"));
} catch (NoSuchAlgorithmException e) {
throw Throwables.propagate(e);
}
}
public static byte[] md5(byte[] in) {
try {
return md5(ByteStreams.newInputStreamSupplier(in));
} catch (IOException e) {
throw Throwables.propagate(e);
}
}
/**
* Computes and returns the MAC value for a supplied input stream. The mac
* object is reset when this method returns successfully.
*
* @param supplier
* the input stream factory
* @param mac
* the mac object
* @return the result of {@link Mac#doFinal()} after updating the mac object
* with all of the bytes in the stream
* @throws IOException
* if an I/O error occurs
*/
public static byte[] mac(InputSupplier<? extends InputStream> supplier, final Mac mac) throws IOException {
return com.google.common.io.ByteStreams.readBytes(checkNotNull(supplier, "supplier"),
new ByteProcessor<byte[]>() {
public boolean processBytes(byte[] buf, int off, int len) {
mac.update(buf, off, len);
return true;
}
public byte[] getResult() {
return mac.doFinal();
}
});
}
/**
* Computes and returns the base64 value for a supplied input stream.
*
* @param supplier
* the input stream factory
*
* @return the result of base 64 encoding all of the bytes in the stream
* @throws IOException
* if an I/O error occurs
*/
public static String base64Encode(InputSupplier<? extends InputStream> supplier) throws IOException {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
return com.google.common.io.ByteStreams.readBytes(InputSuppliers.base64Encoder(supplier),
new ByteProcessor<String>() {
public boolean processBytes(byte[] buf, int off, int len) {
out.write(buf, off, len);
return true;
}
public String getResult() {
return new String(out.toByteArray(), Charsets.UTF_8);
}
});
}
/**
* Computes and returns the unencoded value for an input stream which is
* encoded in Base64.
*
* @param supplier
* the input stream factory
*
* @return the result of base 64 decoding all of the bytes in the stream
* @throws IOException
* if an I/O error occurs
*/
public static byte[] base64Decode(InputSupplier<? extends InputStream> supplier) throws IOException {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
return com.google.common.io.ByteStreams.readBytes(InputSuppliers.base64Decoder(supplier),
new ByteProcessor<byte[]>() {
public boolean processBytes(byte[] buf, int off, int len) {
out.write(buf, off, len);
return true;
}
public byte[] getResult() {
return out.toByteArray();
}
});
}
private static final char[] HEX_CHAR_TABLE = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
};
/**
* Computes and returns the hex value for a supplied input stream.
*
* @param supplier
* the input stream factory
*
* @return the result of hex encoding all of the bytes in the stream
* @throws IOException
* if an I/O error occurs
*/
public static String hexEncode(InputSupplier<? extends InputStream> supplier) throws IOException {
final StringBuilder out = new StringBuilder();
return com.google.common.io.ByteStreams.readBytes(supplier, new ByteProcessor<String>() {
public boolean processBytes(byte[] buf, int off, int len) {
out.append(hex(buf, off, len));
return true;
}
public String getResult() {
return out.toString();
}
});
}
/**
* Computes and returns the unencoded value for an input stream which is
* encoded in hex.
*
* @param supplier
* the input stream factory
*
* @return the result of hex decoding all of the bytes in the stream
* @throws IOException
* if an I/O error occurs
*/
public static byte[] hexDecode(InputSupplier<? extends InputStream> supplier) throws IOException {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
return com.google.common.io.ByteStreams.readBytes(supplier, new ByteProcessor<byte[]>() {
int currentPos = 0;
public boolean processBytes(byte[] buf, int off, int len) {
if (currentPos == 0 && new String(Arrays.copyOfRange(buf, off, 2), Charsets.US_ASCII).equals("0x")) {
off += 2;
}
byte[] decoded = hex(new String(Arrays.copyOfRange(buf, off, len), Charsets.US_ASCII));
out.write(decoded, 0, decoded.length);
currentPos += len;
return true;
}
public byte[] getResult() {
return out.toByteArray();
}
});
}
}

View File

@ -0,0 +1,57 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License 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 org.jclouds.crypto;
import static com.google.common.base.Preconditions.checkNotNull;
import javax.crypto.Mac;
import com.google.common.annotations.Beta;
import com.google.common.io.ByteProcessor;
/**
* functions for {@link Mac}
*
* @author Adrian Cole
*/
@Beta
public class Macs {
/**
* Computes and returns the MAC value for a supplied input stream.
*
* @param mac
* the mac object
* @return the result of {@link Mac#doFinal()} on {@link ByteProcessor#getResult()}
*/
public static ByteProcessor<byte[]> asByteProcessor(final Mac mac) {
checkNotNull(mac, "mac");
return new ByteProcessor<byte[]>() {
public boolean processBytes(byte[] buf, int off, int len) {
mac.update(buf, off, len);
return true;
}
public byte[] getResult() {
return mac.doFinal();
}
};
}
}

View File

@ -65,8 +65,8 @@
package org.jclouds.crypto;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.jclouds.encryption.internal.JCECrypto;
import org.jclouds.javax.annotation.Nullable;
import com.google.common.base.Throwables;
@ -101,19 +101,10 @@ public class Sha512Crypt {
public static enum Function implements com.google.common.base.Function<String, String> {
INSTANCE;
private Crypto crypto;
Function() {
try {
this.crypto = new JCECrypto();
} catch (Exception e) {
Throwables.propagate(e);
}
}
@Override
public String apply(String input) {
return Sha512Crypt.makeShadowLine(input, null, crypto);
return Sha512Crypt.makeShadowLine(input, null);
}
@Override
@ -150,9 +141,9 @@ public class Sha512Crypt {
*
* @return The Sha512 Unix Crypt hash text for the password
*/
public static String makeShadowLine(String password, @Nullable String shadowPrefix, Crypto crypto) {
MessageDigest ctx = crypto.sha512();
MessageDigest alt_ctx = crypto.sha512();
public static String makeShadowLine(String password, @Nullable String shadowPrefix) {
MessageDigest ctx = sha512();
MessageDigest alt_ctx = sha512();
byte[] alt_result;
byte[] temp_result;
@ -331,6 +322,14 @@ public class Sha512Crypt {
return buffer.toString();
}
private static MessageDigest sha512() {
try {
return MessageDigest.getInstance("SHA-512");
} catch (NoSuchAlgorithmException e) {
throw Throwables.propagate(e);
}
}
private static final String b64_from_24bit(byte B2, byte B1, byte B0, int size) {
int v = ((((int) B2) & 0xFF) << 16) | ((((int) B1) & 0xFF) << 8) | ((int) B0 & 0xff);

View File

@ -24,10 +24,8 @@ import static com.google.common.base.Splitter.fixedLength;
import static com.google.common.base.Throwables.propagate;
import static com.google.common.collect.Iterables.get;
import static com.google.common.collect.Iterables.size;
import static com.google.common.io.BaseEncoding.base16;
import static com.google.common.io.BaseEncoding.base64;
import static org.jclouds.crypto.CryptoStreams.base64;
import static org.jclouds.crypto.CryptoStreams.hex;
import static org.jclouds.crypto.CryptoStreams.md5;
import static org.jclouds.crypto.Pems.pem;
import static org.jclouds.crypto.Pems.privateKeySpec;
import static org.jclouds.util.Strings2.toStringAndClose;
@ -56,6 +54,8 @@ import com.google.common.annotations.Beta;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.hash.HashCode;
import com.google.common.hash.Hashing;
import com.google.common.io.InputSupplier;
/**
@ -156,7 +156,7 @@ public class SshKeys {
public static String encodeAsOpenSSH(RSAPublicKey key) {
byte[] keyBlob = keyBlob(key.getPublicExponent(), key.getModulus());
return "ssh-rsa " + base64(keyBlob);
return "ssh-rsa " + base64().encode(keyBlob);
}
/**
@ -270,10 +270,8 @@ public class SshKeys {
*/
public static String sha1(RSAPrivateCrtKeySpec privateKey) {
try {
String sha1 = on(':').join(fixedLength(2).split(
hex(CryptoStreams.sha1(KeyFactory.getInstance("RSA").generatePrivate(privateKey)
.getEncoded()))));
return sha1;
byte[] encodedKey = KeyFactory.getInstance("RSA").generatePrivate(privateKey).getEncoded();
return hexColonDelimited(Hashing.sha1().hashBytes(encodedKey));
} catch (InvalidKeySpecException e) {
throw propagate(e);
} catch (NoSuchAlgorithmException e) {
@ -310,7 +308,11 @@ public class SshKeys {
*/
public static String fingerprint(BigInteger publicExponent, BigInteger modulus) {
byte[] keyBlob = keyBlob(publicExponent, modulus);
return on(':').join(fixedLength(2).split(hex(md5(keyBlob))));
return hexColonDelimited(Hashing.md5().hashBytes(keyBlob));
}
private static String hexColonDelimited(HashCode hc) {
return on(':').join(fixedLength(2).split(base16().lowerCase().encode(hc.asBytes())));
}
public static byte[] keyBlob(BigInteger publicExponent, BigInteger modulus) {

View File

@ -21,7 +21,6 @@ package org.jclouds.encryption.internal;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.KeyPairGenerator;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Provider;
import java.security.cert.CertificateException;
@ -83,57 +82,11 @@ public class JCECrypto implements Crypto {
}
@Override
public MessageDigest digest(String algorithm) throws NoSuchAlgorithmException {
return provider == null ? MessageDigest.getInstance(algorithm) : MessageDigest.getInstance(algorithm, provider);
}
@Override
public Cipher cipher(String algorithm) throws NoSuchAlgorithmException, NoSuchPaddingException {
return provider == null ? Cipher.getInstance(algorithm) : Cipher.getInstance(algorithm, provider);
}
public static final String MD5 = "MD5";
public static final String SHA1 = "SHA1";
public static final String SHA256 = "SHA-256";
public static final String SHA512 = "SHA-512";
@Override
public MessageDigest md5() {
try {
return digest(MD5);
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException("MD5 must be supported", e);
}
}
@Override
public MessageDigest sha1() {
try {
return digest(SHA1);
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException("MD5 must be supported", e);
}
}
@Override
public MessageDigest sha256() {
try {
return digest(SHA256);
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(SHA256 + " must be supported", e);
}
}
@Override
public MessageDigest sha512() {
try {
return digest(SHA512);
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(SHA512 + " must be supported", e);
}
}
public static final String HmacSHA256 = "HmacSHA256";
public static final String HmacSHA1 = "HmacSHA1";

View File

@ -25,6 +25,7 @@ import static com.google.common.base.Throwables.propagate;
import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.get;
import static com.google.common.collect.Iterables.size;
import static com.google.common.io.BaseEncoding.base64;
import static com.google.common.io.ByteStreams.toByteArray;
import static com.google.common.io.Closeables.closeQuietly;
import static com.google.common.net.HttpHeaders.CONTENT_DISPOSITION;
@ -44,7 +45,6 @@ import javax.inject.Named;
import javax.inject.Singleton;
import org.jclouds.Constants;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.io.ContentMetadata;
import org.jclouds.io.MutableContentMetadata;
import org.jclouds.io.Payload;
@ -219,7 +219,7 @@ public class HttpUtils {
.getContentLength());
byte[] md5 = message.getPayload().getContentMetadata().getContentMD5();
if (md5 != null)
logger.debug("%s %s: %s", prefix, CONTENT_MD5, CryptoStreams.base64(md5));
logger.debug("%s %s: %s", prefix, CONTENT_MD5, base64().encode(md5));
if (message.getPayload().getContentMetadata().getContentDisposition() != null)
logger.debug("%s %s: %s", prefix, CONTENT_DISPOSITION, message.getPayload().getContentMetadata()
.getContentDisposition());
@ -282,7 +282,7 @@ public class HttpUtils {
}
public static String nullToEmpty(byte[] md5) {
return md5 != null ? CryptoStreams.base64(md5) : "";
return md5 != null ? base64().encode(md5) : "";
}
public static String nullToEmpty(Collection<String> collection) {

View File

@ -17,14 +17,16 @@
* under the License.
*/
package org.jclouds.http.filters;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.io.BaseEncoding.base64;
import static com.google.common.net.HttpHeaders.AUTHORIZATION;
import static java.lang.String.format;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.crypto.Crypto;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.http.HttpException;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpRequestFilter;
@ -45,9 +47,14 @@ public class BasicAuthentication implements HttpRequestFilter {
@Inject
public BasicAuthentication(@Identity String user, @Credential String password, Crypto crypto) {
this.header = "Basic "
+ CryptoStreams.base64(String.format("%s:%s", checkNotNull(user, "user"),
checkNotNull(password, "password")).getBytes());
checkNotNull(user, "user");
checkNotNull(password, "password");
this.header = basic(user, password);
}
public static String basic(String user, String password) {
return new StringBuilder("Basic ").append(base64().encode(format("%s:%s", user, password).getBytes(UTF_8)))
.toString();
}
@Override

View File

@ -0,0 +1,53 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License 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 org.jclouds.io;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.IOException;
import java.io.InputStream;
import com.google.common.annotations.Beta;
import com.google.common.io.ByteSource;
/**
* functions related to or replacing those in {@link ByteSource}
*
* @author Adrian Cole
*/
@Beta
public class ByteSources {
/**
* always returns the same stream
*
* @param in
* stream to always return
*/
public static ByteSource asByteSource(final InputStream in) {
checkNotNull(in, "in");
return new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return in;
}
};
}
}

View File

@ -1,6 +1,7 @@
package org.jclouds.io;
import static com.google.common.collect.Iterables.any;
import static com.google.common.io.BaseEncoding.base64;
import static com.google.common.net.HttpHeaders.CONTENT_DISPOSITION;
import static com.google.common.net.HttpHeaders.CONTENT_ENCODING;
import static com.google.common.net.HttpHeaders.CONTENT_LANGUAGE;
@ -14,7 +15,6 @@ import java.util.Map.Entry;
import javax.annotation.Resource;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.date.DateCodec;
import org.jclouds.date.DateCodecFactory;
import org.jclouds.io.ContentMetadataCodec.DefaultContentMetadataCodec;
@ -81,7 +81,7 @@ public interface ContentMetadataCodec {
if (md.getContentLength() != null)
builder.put(CONTENT_LENGTH, md.getContentLength() + "");
if (md.getContentMD5() != null)
builder.put(CONTENT_MD5, CryptoStreams.base64(md.getContentMD5()));
builder.put(CONTENT_MD5, base64().encode(md.getContentMD5()));
if (md.getExpires() != null)
builder.put(EXPIRES, getExpiresDateCodec().toString(md.getExpires()));
return builder.build();
@ -99,7 +99,7 @@ public interface ContentMetadataCodec {
if (!chunked && CONTENT_LENGTH.equalsIgnoreCase(header.getKey())) {
contentMetadata.setContentLength(Long.valueOf(header.getValue()));
} else if (CONTENT_MD5.equalsIgnoreCase(header.getKey())) {
contentMetadata.setContentMD5(CryptoStreams.base64(header.getValue()));
contentMetadata.setContentMD5(base64().decode(header.getValue()));
} else if (CONTENT_TYPE.equalsIgnoreCase(header.getKey())) {
contentMetadata.setContentType(header.getValue());
} else if (CONTENT_DISPOSITION.equalsIgnoreCase(header.getKey())) {

View File

@ -23,8 +23,6 @@ import static com.google.common.base.Preconditions.checkNotNull;
import java.io.IOException;
import java.io.InputStream;
import org.jclouds.encoding.internal.FlexBase64;
import com.google.common.annotations.Beta;
import com.google.common.base.Charsets;
import com.google.common.io.ByteStreams;
@ -37,29 +35,6 @@ import com.google.common.io.InputSupplier;
*/
@Beta
public class InputSuppliers {
/**
* base64 encodes bytes from the supplied supplier as they are read.
*/
public static InputSupplier<InputStream> base64Encoder(final InputSupplier<? extends InputStream> supplier) throws IOException {
return new InputSupplier<InputStream>() {
@Override
public InputStream getInput() throws IOException {
return FlexBase64.createEncoderInputStream(supplier.getInput(), 8192, false);
}
};
}
/**
* base64 decodes bytes from the supplied supplier as they are read.
*/
public static InputSupplier<InputStream> base64Decoder(final InputSupplier<? extends InputStream> supplier) throws IOException {
return new InputSupplier<InputStream>() {
@Override
public InputStream getInput() throws IOException {
return FlexBase64.createDecoderInputStream(supplier.getInput());
}
};
}
public static InputSupplier<? extends InputStream> of(final InputStream in) {
checkNotNull(in, "in");

View File

@ -17,24 +17,21 @@
* under the License.
*/
package org.jclouds.io;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.hash.Hashing.md5;
import static com.google.common.io.ByteStreams.toByteArray;
import static org.jclouds.io.ByteSources.asByteSource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.io.payloads.ByteArrayPayload;
import org.jclouds.io.payloads.FilePayload;
import org.jclouds.io.payloads.InputStreamPayload;
import org.jclouds.io.payloads.StringPayload;
import org.jclouds.io.payloads.UrlEncodedFormPayload;
import com.google.common.base.Throwables;
import com.google.common.collect.Multimap;
/**
@ -90,12 +87,10 @@ public class Payloads {
*
* @param payload
* payload to calculate
* @param md5
* digester to calculate payloads with.
* @return new Payload with md5 set.
* @throws IOException
*/
public static Payload calculateMD5(Payload payload, MessageDigest md5) throws IOException {
public static Payload calculateMD5(Payload payload) throws IOException {
checkNotNull(payload, "payload");
if (!payload.isRepeatable()) {
MutableContentMetadata oldContentMetadata = payload.getContentMetadata();
@ -109,48 +104,18 @@ public class Payloads {
oldContentMetadata.setContentMD5(payload.getContentMetadata().getContentMD5());
payload.setContentMetadata(oldContentMetadata);
}
payload.getContentMetadata().setContentMD5(CryptoStreams.digest(payload, md5));
payload.getContentMetadata().setContentMD5(asByteSource(payload.getInput()).hash(md5()).asBytes());
return payload;
}
/**
* Uses default md5 generator.
*
* @see #calculateMD5(Payload, MessageDigest)
*/
public static Payload calculateMD5(Payload payload) throws IOException {
try {
return calculateMD5(payload, MessageDigest.getInstance("MD5"));
} catch (NoSuchAlgorithmException e) {
throw Throwables.propagate(e);
}
}
/**
* Calculates the md5 on a payload, replacing as necessary.
*
* @see #calculateMD5(Payload, MessageDigest)
*/
public static <T extends PayloadEnclosing> T calculateMD5(T payloadEnclosing, MessageDigest md5) throws IOException {
public static <T extends PayloadEnclosing> T calculateMD5(T payloadEnclosing) throws IOException {
checkNotNull(payloadEnclosing, "payloadEnclosing");
Payload newPayload = calculateMD5(payloadEnclosing.getPayload(), md5);
Payload newPayload = calculateMD5(payloadEnclosing.getPayload());
if (newPayload != payloadEnclosing.getPayload())
payloadEnclosing.setPayload(newPayload);
return payloadEnclosing;
}
/**
* Calculates the md5 on a payload, replacing as necessary.
* <p/>
* uses default md5 generator.
*
* @see #calculateMD5(Payload, MessageDigest)
*/
public static <T extends PayloadEnclosing> T calculateMD5(T payloadEnclosing) throws IOException {
try {
return calculateMD5(payloadEnclosing, MessageDigest.getInstance("MD5"));
} catch (NoSuchAlgorithmException e) {
throw Throwables.propagate(e);
}
}
}

View File

@ -18,6 +18,8 @@
*/
package org.jclouds.json.config;
import static com.google.common.io.BaseEncoding.base16;
import java.beans.ConstructorProperties;
import java.io.IOException;
import java.lang.reflect.Type;
@ -32,7 +34,6 @@ import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.date.DateService;
import org.jclouds.domain.JsonBall;
import org.jclouds.json.Json;
@ -165,12 +166,12 @@ public class GsonModule extends AbstractModule {
@Override
public void write(JsonWriter writer, List<Byte> value) throws IOException {
writer.value(CryptoStreams.hex(Bytes.toArray(value)));
writer.value(base16().lowerCase().encode(Bytes.toArray(value)));
}
@Override
public List<Byte> read(JsonReader reader) throws IOException {
return Bytes.asList(CryptoStreams.hex(reader.nextString()));
return Bytes.asList(base16().lowerCase().decode(reader.nextString()));
}
}
@ -180,12 +181,12 @@ public class GsonModule extends AbstractModule {
@Override
public void write(JsonWriter writer, byte[] value) throws IOException {
writer.value(CryptoStreams.hex(value));
writer.value(base16().lowerCase().encode(value));
}
@Override
public byte[] read(JsonReader reader) throws IOException {
return CryptoStreams.hex(reader.nextString());
return base16().lowerCase().decode(reader.nextString());
}
}

View File

@ -1,121 +0,0 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License 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 org.jclouds.crypto;
import static org.testng.Assert.assertEquals;
import java.io.IOException;
import org.jclouds.io.Payloads;
import org.testng.annotations.Test;
import com.google.common.base.Charsets;
/**
*
* @author Adrian Cole
*/
@Test(groups = "unit", singleThreaded = true)
public class CryptoStreamsTest {
@Test
public void testBase64Encode() throws IOException {
String encoded = CryptoStreams.base64Encode(Payloads.newStringPayload("hello world"));
assertEquals(encoded, "aGVsbG8gd29ybGQ=");
}
@Test
public void testBase64Decode() throws IOException {
byte[] decoded = CryptoStreams.base64Decode(Payloads.newStringPayload("aGVsbG8gd29ybGQ="));
assertEquals(new String(decoded, Charsets.UTF_8), "hello world");
}
@Test
public void testBase64DecodeUrlJson() throws IOException {
byte[] decoded = CryptoStreams.base64("eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9");
assertEquals(new String(decoded, Charsets.UTF_8), "{\"alg\":\"RS256\",\"typ\":\"JWT\"}");
}
@Test
public void testBase64DecodeUrlNoPadding() throws IOException {
byte[] decoded = CryptoStreams
.base64("eyJpc3MiOiI3NjEzMjY3OTgwNjktcjVtbGpsbG4xcmQ0bHJiaGc3NWVmZ2lncDM2bTc4ajVAZGV2ZWxvcGVyLmdzZXJ2aWNlYWNjb3VudC5jb20iLCJzY29wZSI6Imh0dHBzOi8vd3d3Lmdvb2dsZWFwaXMuY29tL2F1dGgvcHJlZGljdGlvbiIsImF1ZCI6Imh0dHBzOi8vYWNjb3VudHMuZ29vZ2xlLmNvbS9vL29hdXRoMi90b2tlbiIsImV4cCI6MTMyODU1NDM4NSwiaWF0IjoxMzI4NTUwNzg1fQ");
assertEquals(new String(decoded, Charsets.UTF_8), "{"
+ "\"iss\":\"761326798069-r5mljlln1rd4lrbhg75efgigp36m78j5@developer.gserviceaccount.com\","
+ "\"scope\":\"https://www.googleapis.com/auth/prediction\","
+ "\"aud\":\"https://accounts.google.com/o/oauth2/token\"," + "\"exp\":1328554385," + "\"iat\":1328550785"
+ "}");
}
@Test
public void testBase64EncodeUrlNoSinglePad() {
assertEquals(CryptoStreams.base64("any carnal pleasu".getBytes(Charsets.UTF_8)), "YW55IGNhcm5hbCBwbGVhc3U=");
assertEquals(CryptoStreams.base64Url("any carnal pleasu".getBytes(Charsets.UTF_8)), "YW55IGNhcm5hbCBwbGVhc3U");
}
@Test
public void testBase64EncodeUrlNoDoublePad() {
assertEquals(CryptoStreams.base64("any carnal pleas".getBytes(Charsets.UTF_8)), "YW55IGNhcm5hbCBwbGVhcw==");
assertEquals(CryptoStreams.base64Url("any carnal pleas".getBytes(Charsets.UTF_8)), "YW55IGNhcm5hbCBwbGVhcw");
}
@Test
public void testBase64EncodeUrlHyphenNotPlus() {
assertEquals(CryptoStreams.base64("i?>".getBytes(Charsets.UTF_8)), "aT8+");
assertEquals(CryptoStreams.base64Url("i?>".getBytes(Charsets.UTF_8)), "aT8-");
}
@Test
public void testBase64EncodeUrlUnderscoreNotSlash() {
assertEquals(CryptoStreams.base64("i??".getBytes(Charsets.UTF_8)), "aT8/");
assertEquals(CryptoStreams.base64Url("i??".getBytes(Charsets.UTF_8)), "aT8_");
}
@Test
public void testBase64DecodeWithoutSinglePad() {
String expect = "any carnal pleasu";
String compare = new String(CryptoStreams.base64("YW55IGNhcm5hbCBwbGVhc3U="), Charsets.UTF_8);
assertEquals(compare,expect);
assertEquals(new String(CryptoStreams.base64("YW55IGNhcm5hbCBwbGVhc3U"), Charsets.UTF_8), "any carnal pleasu");
}
@Test
public void testBase64DecodeWithoutDoublePad() {
assertEquals(new String(CryptoStreams.base64("YW55IGNhcm5hbCBwbGVhcw=="), Charsets.UTF_8), "any carnal pleas");
assertEquals(new String(CryptoStreams.base64("YW55IGNhcm5hbCBwbGVhcw"), Charsets.UTF_8), "any carnal pleas");
}
@Test
public void testHexEncode() throws IOException {
String encoded = CryptoStreams.hexEncode(Payloads.newStringPayload("hello world"));
assertEquals(encoded, "68656c6c6f20776f726c64");
}
@Test
public void testHexDecode() throws IOException {
byte[] decoded = CryptoStreams.hexDecode(Payloads.newStringPayload("68656c6c6f20776f726c64"));
assertEquals(new String(decoded, Charsets.UTF_8), "hello world");
}
}

View File

@ -20,27 +20,15 @@ package org.jclouds.crypto;
import static org.testng.Assert.assertEquals;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.google.inject.Guice;
import com.google.inject.Injector;
/**
* @author Adrian Cole
*/
@Test(groups = "unit")
public class Sha512CryptTest {
protected Crypto crypto;
@BeforeTest
protected void createCrypto() {
Injector i = Guice.createInjector();
crypto = i.getInstance(Crypto.class);
}
public static final Object[][] TEST_DATA = {
{ "Hello world!", "$6$saltstring",
"$6$saltstring$svn8UoSVapNtMuq1ukKS4tPQd8iKwSMHWjl/O817G3uBnIFNjnQJuesI68u4OTLiBFdcbYEdFCoEOfaS35inz1" },
@ -72,6 +60,6 @@ public class Sha512CryptTest {
*/
@Test(dataProvider = "data")
public void testMakeCryptedPasswordHash(String password, String salt, String expected) {
assertEquals(Sha512Crypt.makeShadowLine(password, salt, crypto), expected);
assertEquals(Sha512Crypt.makeShadowLine(password, salt), expected);
}
}

View File

@ -1,514 +0,0 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License 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 org.jclouds.encoding.internal;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import org.testng.Assert;
import org.testng.annotations.Test;
import com.google.common.primitives.Bytes;
/**
* @author Jason T. Greene
*/
public class FlexBase64Test {
public static final String TOWEL = "A towel, it says, is about the most massively useful thing an interstellar " +
"hitchhiker can have. Partly it has great practical value - you can wrap it around you for warmth as you " +
"bound across the cold moons of Jaglan Beta; you can lie on it on the brilliant marble-sanded beaches of " +
"Santraginus V, inhaling the heady sea vapours; you can sleep under it beneath the stars which shine so " +
"redly on the desert world of Kakrafoon; use it to sail a mini raft down the slow heavy river Moth; wet " +
"it for use in hand-to- hand-combat; wrap it round your head to ward off noxious fumes or to avoid the " +
"gaze of the Ravenous Bugblatter Beast of Traal (a mindboggingly stupid animal, it assumes that if you " +
"can't see it, it can't see you - daft as a bush, but very ravenous); you can wave your towel in " +
"emergencies as a distress signal, and of course dry yourself off with it if it still seems to be clean " +
"enough." +
"\n\n" +
"More importantly, a towel has immense psychological value. For some reason, if a strag " +
"(strag: non-hitch hiker) discovers that a hitch hiker has his towel with him, he will automatically " +
"assume that he is also in possession of a toothbrush, face flannel, soap, tin of biscuits, flask, compass, " +
"map, ball of string, gnat spray, wet weather gear, space suit etc., etc. Furthermore, the strag will then " +
"happily lend the hitch hiker any of these or a dozen other items that the hitch hiker might accidentally " +
"have \"lost\". What the strag will think is that any man who can hitch the length and breadth of the " +
"galaxy, rough it, slum it, struggle against terrible odds, win through, and still knows where his towel " +
"is is clearly a man to be reckoned with.\n";
public static final String TOWEL_BASE64 =
"QSB0b3dlbCwgaXQgc2F5cywgaXMgYWJvdXQgdGhlIG1vc3QgbWFzc2l2ZWx5IHVzZWZ1bCB0aGlu\r\n"+
"ZyBhbiBpbnRlcnN0ZWxsYXIgaGl0Y2hoaWtlciBjYW4gaGF2ZS4gUGFydGx5IGl0IGhhcyBncmVh\r\n"+
"dCBwcmFjdGljYWwgdmFsdWUgLSB5b3UgY2FuIHdyYXAgaXQgYXJvdW5kIHlvdSBmb3Igd2FybXRo\r\n"+
"IGFzIHlvdSBib3VuZCBhY3Jvc3MgdGhlIGNvbGQgbW9vbnMgb2YgSmFnbGFuIEJldGE7IHlvdSBj\r\n"+
"YW4gbGllIG9uIGl0IG9uIHRoZSBicmlsbGlhbnQgbWFyYmxlLXNhbmRlZCBiZWFjaGVzIG9mIFNh\r\n"+
"bnRyYWdpbnVzIFYsIGluaGFsaW5nIHRoZSBoZWFkeSBzZWEgdmFwb3VyczsgeW91IGNhbiBzbGVl\r\n"+
"cCB1bmRlciBpdCBiZW5lYXRoIHRoZSBzdGFycyB3aGljaCBzaGluZSBzbyByZWRseSBvbiB0aGUg\r\n"+
"ZGVzZXJ0IHdvcmxkIG9mIEtha3JhZm9vbjsgdXNlIGl0IHRvIHNhaWwgYSBtaW5pIHJhZnQgZG93\r\n"+
"biB0aGUgc2xvdyBoZWF2eSByaXZlciBNb3RoOyB3ZXQgaXQgZm9yIHVzZSBpbiBoYW5kLXRvLSBo\r\n"+
"YW5kLWNvbWJhdDsgd3JhcCBpdCByb3VuZCB5b3VyIGhlYWQgdG8gd2FyZCBvZmYgbm94aW91cyBm\r\n"+
"dW1lcyBvciB0byBhdm9pZCB0aGUgZ2F6ZSBvZiB0aGUgUmF2ZW5vdXMgQnVnYmxhdHRlciBCZWFz\r\n"+
"dCBvZiBUcmFhbCAoYSBtaW5kYm9nZ2luZ2x5IHN0dXBpZCBhbmltYWwsIGl0IGFzc3VtZXMgdGhh\r\n"+
"dCBpZiB5b3UgY2FuJ3Qgc2VlIGl0LCBpdCBjYW4ndCBzZWUgeW91IC0gZGFmdCBhcyBhIGJ1c2gs\r\n"+
"IGJ1dCB2ZXJ5IHJhdmVub3VzKTsgeW91IGNhbiB3YXZlIHlvdXIgdG93ZWwgaW4gZW1lcmdlbmNp\r\n"+
"ZXMgYXMgYSBkaXN0cmVzcyBzaWduYWwsIGFuZCBvZiBjb3Vyc2UgZHJ5IHlvdXJzZWxmIG9mZiB3\r\n"+
"aXRoIGl0IGlmIGl0IHN0aWxsIHNlZW1zIHRvIGJlIGNsZWFuIGVub3VnaC4KCk1vcmUgaW1wb3J0\r\n"+
"YW50bHksIGEgdG93ZWwgaGFzIGltbWVuc2UgcHN5Y2hvbG9naWNhbCB2YWx1ZS4gRm9yIHNvbWUg\r\n"+
"cmVhc29uLCBpZiBhIHN0cmFnIChzdHJhZzogbm9uLWhpdGNoIGhpa2VyKSBkaXNjb3ZlcnMgdGhh\r\n"+
"dCBhIGhpdGNoIGhpa2VyIGhhcyBoaXMgdG93ZWwgd2l0aCBoaW0sIGhlIHdpbGwgYXV0b21hdGlj\r\n"+
"YWxseSBhc3N1bWUgdGhhdCBoZSBpcyBhbHNvIGluIHBvc3Nlc3Npb24gb2YgYSB0b290aGJydXNo\r\n"+
"LCBmYWNlIGZsYW5uZWwsIHNvYXAsIHRpbiBvZiBiaXNjdWl0cywgZmxhc2ssIGNvbXBhc3MsIG1h\r\n"+
"cCwgYmFsbCBvZiBzdHJpbmcsIGduYXQgc3ByYXksIHdldCB3ZWF0aGVyIGdlYXIsIHNwYWNlIHN1\r\n"+
"aXQgZXRjLiwgZXRjLiBGdXJ0aGVybW9yZSwgdGhlIHN0cmFnIHdpbGwgdGhlbiBoYXBwaWx5IGxl\r\n"+
"bmQgdGhlIGhpdGNoIGhpa2VyIGFueSBvZiB0aGVzZSBvciBhIGRvemVuIG90aGVyIGl0ZW1zIHRo\r\n"+
"YXQgdGhlIGhpdGNoIGhpa2VyIG1pZ2h0IGFjY2lkZW50YWxseSBoYXZlICJsb3N0Ii4gV2hhdCB0\r\n"+
"aGUgc3RyYWcgd2lsbCB0aGluayBpcyB0aGF0IGFueSBtYW4gd2hvIGNhbiBoaXRjaCB0aGUgbGVu\r\n"+
"Z3RoIGFuZCBicmVhZHRoIG9mIHRoZSBnYWxheHksIHJvdWdoIGl0LCBzbHVtIGl0LCBzdHJ1Z2ds\r\n"+
"ZSBhZ2FpbnN0IHRlcnJpYmxlIG9kZHMsIHdpbiB0aHJvdWdoLCBhbmQgc3RpbGwga25vd3Mgd2hl\r\n"+
"cmUgaGlzIHRvd2VsIGlzIGlzIGNsZWFybHkgYSBtYW4gdG8gYmUgcmVja29uZWQgd2l0aC4K\r\n";
private static final String KNOWLEDGE =
"Man is distinguished, not only by his reason, but by this singular passion from " +
"other animals, which is a lust of the mind, that by a perseverance of delight " +
"in the continued and indefatigable generation of knowledge, exceeds the short " +
"vehemence of any carnal pleasure.";
private static final String KNOWLEDGE_ENCODED =
"TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\r\n" +
"IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg\r\n" +
"dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu\r\n" +
"dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo\r\n" +
"ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=\r\n";
@Test
public void testEncoderDecoder() throws IOException {
byte[] nums = new byte[32768];
for (int i = 0; i < 32768; i++) {
nums[i] = (byte) (i % 255);
}
byte[] output = new byte[65535];
FlexBase64.Encoder encoder = FlexBase64.createEncoder(true);
int last = encoder.encode(nums, 0, nums.length, output, 0, output.length);
last = encoder.complete(output, last);
byte[] decode = new byte[nums.length];
FlexBase64.Decoder decoder = FlexBase64.createDecoder();
last = decoder.decode(output, 0, last, decode, 0, decode.length);
Assert.assertEquals(nums.length, last);
for (int i = 0; i < last; i++) {
Assert.assertEquals(nums[i], decode[i]);
}
}
@Test
public void testEncoderDecoderBuffer() throws IOException {
byte[] nums = new byte[32768];
for (int i = 0; i < 32768; i++) {
nums[i] = (byte) (i % 255);
}
ByteBuffer source = ByteBuffer.wrap(nums);
ByteBuffer target = ByteBuffer.allocate(65535);
FlexBase64.Encoder encoder = FlexBase64.createEncoder(true);
encoder.encode(source, target);
encoder.complete(target);
ByteBuffer decoded = ByteBuffer.allocate(nums.length);
FlexBase64.Decoder decoder = FlexBase64.createDecoder();
target.flip();
decoder.decode(target, decoded);
decoded.flip();
Assert.assertEquals(nums.length, decoded.remaining());
for (int i = 0; i < nums.length; i++) {
Assert.assertEquals(nums[i], decoded.get());
}
}
@Test
public void testDrain() throws IOException {
byte[] bytes = "c3VyZS4=\r\n\r\n!".getBytes("US-ASCII");
ByteBuffer source = ByteBuffer.wrap(bytes);
ByteBuffer target = ByteBuffer.allocateDirect(100);
FlexBase64.createDecoder().decode(source, target);
Assert.assertEquals((byte) '\r' & 0xFF, source.get() & 0xFF);
Assert.assertEquals((byte) '\n' & 0xFF, source.get() & 0xFF);
Assert.assertEquals((byte) '!' & 0xFF, source.get() & 0xFF);
byte[] dest = new byte[100];
FlexBase64.Decoder decoder = FlexBase64.createDecoder();
decoder.decode(bytes, 0, bytes.length, dest, 0, dest.length);
Assert.assertEquals(10, decoder.getLastInputPosition());
bytes = "YXN1cmUu\r\n\r\nA".getBytes("US-ASCII");
dest = new byte[6];
decoder = FlexBase64.createDecoder();
decoder.decode(bytes, 0, bytes.length, dest, 0, dest.length);
Assert.assertEquals(12, decoder.getLastInputPosition());
}
@Test
public void testEncoderDecoderBufferLoops() throws IOException {
byte[] nums = new byte[32768];
for (int i = 0; i < 32768; i++) {
nums[i] = (byte) (i % 255);
}
ByteBuffer source = ByteBuffer.wrap(nums);
ByteBuffer target = ByteBuffer.allocate(65535);
FlexBase64.Encoder encoder = FlexBase64.createEncoder(true);
int limit = target.limit();
target.limit(100);
while (source.remaining() > 0) {
encoder.encode(source, target);
int add = limit - target.position();
add = add < 100 ? add : 100;
target.limit(target.limit() + add);
}
encoder.complete(target);
ByteBuffer decoded = ByteBuffer.allocate(nums.length);
FlexBase64.Decoder decoder = FlexBase64.createDecoder();
target.flip();
limit = decoded.limit();
decoded.limit(100);
while (target.remaining() > 0) {
decoder.decode(target, decoded);
int add = limit - decoded.position();
add = add < 100 ? add : 100;
decoded.limit(decoded.position() + add);
}
decoded.flip();
Assert.assertEquals(nums.length, decoded.remaining());
for (int i = 0; i < nums.length; i++) {
Assert.assertEquals(nums[i], decoded.get());
}
}
@Test
public void testEncoderDecoderLoopWithOffset() throws IOException {
byte[] nums = new byte[32768];
for (int i = 0; i < 32768; i++) {
nums[i] = (byte) (i % 255);
}
byte[] output = new byte[65535];
FlexBase64.Encoder encoder = FlexBase64.createEncoder(true);
int opos = 5;
int pos = 0;
while (pos < 32768) {
opos = encoder.encode(nums, pos, nums.length, output, opos, opos + 10000);
pos = encoder.getLastInputPosition();
}
opos = encoder.complete(output, opos);
byte[] decode = new byte[nums.length];
FlexBase64.Decoder decoder = FlexBase64.createDecoder();
int stop = opos;
pos = 5;
int last = 0;
while (pos < stop) {
last = decoder.decode(output, pos, stop, decode, last, last + 10000);
pos = decoder.getLastInputPosition();
}
Assert.assertEquals(nums.length, last);
for (int i = 0; i < last; i++) {
Assert.assertEquals(nums[i], decode[i]);
}
}
@Test
public void testEncodeString() throws Exception {
byte[] data = ("Man is distinguished, not only by his reason, but by this singular passion from "
+ "other animals, which is a lust of the mind, that by a perseverance of delight "
+ "in the continued and indefatigable generation of knowledge, exceeds the short "
+ "vehemence of any carnal pleasure.").getBytes("US-ASCII");
String expect = "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\r\n"
+ "IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg\r\n"
+ "dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu\r\n"
+ "dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo\r\n"
+ "ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=\r\n";
Assert.assertEquals(expect, FlexBase64.encodeString(data, true));
Assert.assertEquals(expect, FlexBase64.encodeString(ByteBuffer.wrap(data), true));
byte[] data2 = new byte[data.length + 10];
System.arraycopy(data, 0, data2, 5, data.length);
Assert.assertEquals(expect, FlexBase64.encodeString(data2, 5, data.length + 5, true));
}
@Test
public void testEncodeBytes() throws Exception {
byte[] data = ("Man is distinguished, not only by his reason, but by this singular passion from "
+ "other animals, which is a lust of the mind, that by a perseverance of delight "
+ "in the continued and indefatigable generation of knowledge, exceeds the short "
+ "vehemence of any carnal pleasure.").getBytes("US-ASCII");
byte[] expect = ("TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\r\n"
+ "IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg\r\n"
+ "dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu\r\n"
+ "dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo\r\n"
+ "ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=\r\n").getBytes("US-ASCII");
Assert.assertEquals(Bytes.asList(expect), Bytes.asList(FlexBase64.encodeBytes(data, 0, data.length, true)));
}
@Test
public void testDecodeString() throws Exception {
String expect = "Man is distinguished, not only by his reason, but by this singular passion from "
+ "other animals, which is a lust of the mind, that by a perseverance of delight "
+ "in the continued and indefatigable generation of knowledge, exceeds the short "
+ "vehemence of any carnal pleasure.";
String encoded = "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\r\n"
+ "IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg\r\n"
+ "dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu\r\n"
+ "dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo\r\n"
+ "ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=\r\n";
ByteBuffer buffer = FlexBase64.decode(encoded);
Assert.assertEquals(expect, new String(buffer.array(), buffer.arrayOffset(), buffer.limit(), "US-ASCII"));
buffer = FlexBase64.decode(ByteBuffer.wrap(encoded.getBytes("US-ASCII")));
Assert.assertEquals(expect, new String(buffer.array(), buffer.arrayOffset(), buffer.limit(), "US-ASCII"));
buffer = FlexBase64.decode(encoded.getBytes("US-ASCII"), 0, encoded.length());
Assert.assertEquals(expect, new String(buffer.array(), buffer.arrayOffset(), buffer.limit(), "US-ASCII"));
byte[] output = new byte[expect.length()];
FlexBase64.createDecoder().decode(encoded, output);
Assert.assertEquals(expect, new String(output, 0, output.length, "US-ASCII"));
}
@Test
public void testURLString() throws Exception {
byte[] source = {0x6b, (byte) 0xf6, (byte) 0xfe};
Assert.assertEquals("a_b-", FlexBase64.encodeURLString(source, 0, 3));
Assert.assertEquals(Bytes.asList(source), Bytes.asList(FlexBase64.decode("a_b-").array()));
String actual = FlexBase64.encodeURLString("test".getBytes("UTF-8"), 0, 4);
Assert.assertEquals("dGVzdA", actual);
ByteBuffer decode = FlexBase64.decode(actual);
Assert.assertEquals("test", new String(decode.array(), 0, decode.limit(), "UTF-8"));
byte[] bytes = TOWEL.getBytes("UTF-8");
Assert.assertEquals(TOWEL_BASE64.replace("\r\n",""), FlexBase64.encodeURLString(ByteBuffer.wrap(bytes)));
bytes = KNOWLEDGE.getBytes("UTF-8");
String replace = KNOWLEDGE_ENCODED.replace("\r\n", "");
Assert.assertEquals(replace.substring(0, replace.length() - 1), FlexBase64.encodeURLString(ByteBuffer.wrap(bytes)));
}
@Test
public void testEncoderInputStream() throws Exception {
FlexBase64.EncoderInputStream encoderInputStream = FlexBase64.createEncoderInputStream(new ByteArrayInputStream(
TOWEL.getBytes("US-ASCII")));
ByteBuffer base64 = ByteBuffer.wrap(TOWEL_BASE64.getBytes("US-ASCII"));
verifyStreamContents(encoderInputStream, base64);
encoderInputStream = FlexBase64.createEncoderInputStream(new ByteArrayInputStream(TOWEL.getBytes("US-ASCII")),
8192, false);
base64 = ByteBuffer.wrap(TOWEL_BASE64.replace("\r\n", "").getBytes("US-ASCII"));
verifyStreamContents(encoderInputStream, base64);
encoderInputStream = FlexBase64.createEncoderInputStream(new ByteArrayInputStream(TOWEL.getBytes("US-ASCII")));
base64 = ByteBuffer.wrap(TOWEL_BASE64.getBytes("US-ASCII"));
verifyStreamContentsOneByte(encoderInputStream, base64);
encoderInputStream = FlexBase64.createEncoderInputStream(new ByteArrayInputStream(TOWEL.getBytes("US-ASCII")),
8192, false);
base64 = ByteBuffer.wrap(TOWEL_BASE64.replace("\r\n", "").getBytes("US-ASCII"));
verifyStreamContentsOneByte(encoderInputStream, base64);
}
@Test
public void testDecoderInputStream() throws Exception {
FlexBase64.DecoderInputStream stream = FlexBase64.createDecoderInputStream(new ByteArrayInputStream(TOWEL_BASE64
.getBytes("US-ASCII")));
ByteBuffer base64 = ByteBuffer.wrap(TOWEL.getBytes("US-ASCII"));
verifyStreamContents(stream, base64);
stream = FlexBase64.createDecoderInputStream(new ByteArrayInputStream(TOWEL_BASE64.replace("\r\n", "").getBytes(
"US-ASCII")));
base64 = ByteBuffer.wrap(TOWEL.getBytes("US-ASCII"));
verifyStreamContents(stream, base64);
stream = FlexBase64.createDecoderInputStream(new ByteArrayInputStream(TOWEL_BASE64.getBytes("US-ASCII")));
base64 = ByteBuffer.wrap(TOWEL.getBytes("US-ASCII"));
verifyStreamContentsOneByte(stream, base64);
stream = FlexBase64.createDecoderInputStream(new ByteArrayInputStream(TOWEL_BASE64.replace("\r\n", "").getBytes(
"US-ASCII")));
base64 = ByteBuffer.wrap(TOWEL.replace("\r\n", "").getBytes("US-ASCII"));
verifyStreamContentsOneByte(stream, base64);
stream = FlexBase64.createDecoderInputStream(
new ByteArrayInputStream(TOWEL_BASE64.replace("\r\n", "").getBytes("US-ASCII")), 10);
base64 = ByteBuffer.wrap(TOWEL.replace("\r\n", "").getBytes("US-ASCII"));
verifyStreamContentsOneByte(stream, base64);
}
@Test
public void testEncoderOutputStream() throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FlexBase64.EncoderOutputStream stream = FlexBase64.createEncoderOutputStream(baos);
byte[] towel = TOWEL.getBytes("US-ASCII");
stream.write(towel);
stream.close();
Assert.assertEquals(Bytes.asList(TOWEL_BASE64.getBytes("US-ASCII")), Bytes.asList(baos.toByteArray()));
baos.reset();
stream = FlexBase64.createEncoderOutputStream(baos, 8192, false);
stream.write(towel);
stream.close();
Assert.assertEquals(Bytes.asList(TOWEL_BASE64.replace("\r\n", "").getBytes("US-ASCII")), Bytes.asList(baos.toByteArray()));
baos.reset();
stream = FlexBase64.createEncoderOutputStream(baos, 8192, true);
chunkWrite(stream, towel);
Assert.assertEquals(Bytes.asList(TOWEL_BASE64.getBytes("US-ASCII")), Bytes.asList(baos.toByteArray()));
baos.reset();
stream = FlexBase64.createEncoderOutputStream(baos, 8192, false);
chunkWrite(stream, towel);
Assert.assertEquals(Bytes.asList(TOWEL_BASE64.replace("\r\n", "").getBytes("US-ASCII")), Bytes.asList(baos.toByteArray()));
baos.reset();
stream = FlexBase64.createEncoderOutputStream(baos, 8192, true);
oneByteWrite(stream, towel);
Assert.assertEquals(Bytes.asList(TOWEL_BASE64.getBytes("US-ASCII")), Bytes.asList(baos.toByteArray()));
baos.reset();
stream = FlexBase64.createEncoderOutputStream(baos, 8192, false);
oneByteWrite(stream, towel);
Assert.assertEquals(Bytes.asList(TOWEL_BASE64.replace("\r\n", "").getBytes("US-ASCII")), Bytes.asList(baos.toByteArray()));
}
@Test
public void testDecoderOutputStream() throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FlexBase64.DecoderOutputStream stream = FlexBase64.createDecoderOutputStream(baos);
byte[] towel = TOWEL_BASE64.getBytes("US-ASCII");
byte[] towelStrip = TOWEL_BASE64.replace("\r\n", "").getBytes("US-ASCII");
stream.write(towel);
stream.close();
Assert.assertEquals(Bytes.asList(TOWEL.getBytes("US-ASCII")), Bytes.asList(baos.toByteArray()));
baos.reset();
stream = FlexBase64.createDecoderOutputStream(baos);
stream.write(towelStrip);
stream.close();
Assert.assertEquals(Bytes.asList(TOWEL.getBytes("US-ASCII")), Bytes.asList(baos.toByteArray()));
baos.reset();
stream = FlexBase64.createDecoderOutputStream(baos);
chunkWrite(stream, towel);
Assert.assertEquals(Bytes.asList(TOWEL.getBytes("US-ASCII")), Bytes.asList(baos.toByteArray()));
baos.reset();
stream = FlexBase64.createDecoderOutputStream(baos);
chunkWrite(stream, towelStrip);
Assert.assertEquals(Bytes.asList(TOWEL.getBytes("US-ASCII")), Bytes.asList(baos.toByteArray()));
baos.reset();
stream = FlexBase64.createDecoderOutputStream(baos);
oneByteWrite(stream, towel);
Assert.assertEquals(Bytes.asList(TOWEL.getBytes("US-ASCII")), Bytes.asList(baos.toByteArray()));
baos.reset();
stream = FlexBase64.createDecoderOutputStream(baos);
oneByteWrite(stream, towelStrip);
Assert.assertEquals(Bytes.asList(TOWEL.getBytes("US-ASCII")), Bytes.asList(baos.toByteArray()));
baos.reset();
stream = FlexBase64.createDecoderOutputStream(baos, 10);
chunkWrite(stream, towelStrip);
Assert.assertEquals(Bytes.asList(TOWEL.getBytes("US-ASCII")), Bytes.asList(baos.toByteArray()));
}
private void chunkWrite(OutputStream stream, byte[] towel) throws IOException {
ByteBuffer wrap = ByteBuffer.wrap(towel);
int remaining = wrap.remaining();
while (remaining > 0) {
int left = remaining < 100 ? remaining : 100;
byte[] chunk = new byte[left];
wrap.get(chunk);
stream.write(chunk);
remaining = wrap.remaining();
}
stream.close();
}
private void oneByteWrite(OutputStream stream, byte[] towel) throws IOException {
ByteBuffer wrap = ByteBuffer.wrap(towel);
while (wrap.remaining() > 0) {
stream.write(wrap.get() & 0xFF);
}
stream.close();
}
private void verifyStreamContentsOneByte(InputStream inputStream, ByteBuffer base64) throws IOException {
int read = inputStream.read();
while (read > -1) {
byte expected = base64.get();
Assert.assertEquals(expected, read);
read = inputStream.read();
}
Assert.assertEquals(0, base64.remaining());
}
private void verifyStreamContents(InputStream inputStream, ByteBuffer base64) throws IOException {
byte[] buffer = new byte[100];
int read = inputStream.read(buffer);
while (read > -1) {
for (int i = 0; i < read; i++) {
byte expected = base64.get();
byte actual = buffer[i];
Assert.assertEquals(expected, actual);
}
read = inputStream.read(buffer);
}
Assert.assertEquals(0, base64.remaining());
}
}

View File

@ -18,10 +18,10 @@
*/
package org.jclouds.http;
import static com.google.common.hash.Hashing.md5;
import static com.google.common.io.BaseEncoding.base64;
import static com.google.common.io.Closeables.closeQuietly;
import static java.lang.String.format;
import static org.jclouds.crypto.CryptoStreams.base64;
import static org.jclouds.crypto.CryptoStreams.md5Base64;
import static org.jclouds.http.options.GetOptions.Builder.tail;
import static org.jclouds.io.Payloads.newFilePayload;
import static org.jclouds.io.Payloads.newStringPayload;
@ -29,25 +29,26 @@ import static org.jclouds.util.Throwables2.getFirstThrowableOfType;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.net.URI;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.atomic.AtomicInteger;
import org.jclouds.io.InputSuppliers;
import org.jclouds.io.Payload;
import org.jclouds.util.Strings2;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Multimap;
import com.google.common.io.ByteSource;
import com.google.common.io.CharSink;
import com.google.common.io.Files;
/**
* Tests for functionality all {@link HttpCommandExecutorService http executor
@ -115,7 +116,7 @@ public abstract class BaseHttpCommandExecutorServiceIntegrationTest extends Base
public void testGetBigFile() throws IOException {
InputStream input = getConsitution();
try {
assertEquals(md5Base64(InputSuppliers.of(input)), md5);
assertValidMd5(input);
} catch (RuntimeException e) {
closeQuietly(input);
// since we are parsing client side, and not through a response
@ -123,13 +124,22 @@ public abstract class BaseHttpCommandExecutorServiceIntegrationTest extends Base
// lightning doesn't strike twice in the same spot.
if (getFirstThrowableOfType(e, IOException.class) != null) {
input = getConsitution();
assertEquals(md5Base64(InputSuppliers.of(input)), md5);
assertValidMd5(input);
}
} finally {
closeQuietly(input);
}
}
private void assertValidMd5(final InputStream input) throws IOException {
assertEquals(base64().encode(new ByteSource() {
@Override
public InputStream openStream() {
return input;
}
}.hash(md5()).asBytes()), md5);
}
private InputStream getConsitution() {
URI constitutionUri = URI.create(format("http://localhost:%d/101constitutions", testPort));
return context.utils().http().get(constitutionUri);
@ -142,23 +152,24 @@ public abstract class BaseHttpCommandExecutorServiceIntegrationTest extends Base
@Test(invocationCount = 1)
public void testUploadBigFile() throws IOException {
String filename = "jclouds";
OutputStream os = null;
File f = null;
try {
// create a file, twice big as free heap memory
f = File.createTempFile(filename, "tmp");
f.deleteOnExit();
long length = (long) (Runtime.getRuntime().freeMemory() * 1.1);
os = new BufferedOutputStream(new FileOutputStream(f.getAbsolutePath()));
MessageDigest digester = context.utils().crypto().md5();
MessageDigest digester = md5Digest();
ByteArrayOutputStream out = new ByteArrayOutputStream();
CharSink fileSink = Files.asCharSink(f, Charsets.UTF_8);
Writer out = null;
try {
out = fileSink.openStream();
for (long i = 0; i < length; i++) {
out.append('a');
digester.update((byte) 'a');
os.write((byte) 'a');
}
os.flush();
out.flush();
} finally {
closeQuietly(out);
}
@ -167,15 +178,22 @@ public abstract class BaseHttpCommandExecutorServiceIntegrationTest extends Base
byte[] digest = digester.digest();
payload.getContentMetadata().setContentMD5(digest);
Multimap<String, String> headers = client.postPayloadAndReturnHeaders("", payload);
assertEquals(headers.get("x-Content-MD5"), ImmutableList.of(base64(digest)));
assertEquals(headers.get("x-Content-MD5"), ImmutableList.of(base64().encode(digest)));
payload.release();
} finally {
closeQuietly(os);
if (f != null && f.exists())
f.delete();
}
}
private MessageDigest md5Digest() throws AssertionError {
try {
return MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new AssertionError(e);
}
}
@Test(invocationCount = 5, timeOut = 5000)
public void testPost() {
assertEquals(client.post("", "foo").trim(), "fooPOST");

View File

@ -19,6 +19,8 @@
package org.jclouds.http;
import static com.google.common.base.Throwables.getStackTraceAsString;
import static com.google.common.hash.Hashing.md5;
import static com.google.common.io.BaseEncoding.base64;
import static com.google.common.io.ByteStreams.copy;
import static com.google.common.io.ByteStreams.join;
import static com.google.common.io.ByteStreams.newInputStreamSupplier;
@ -32,7 +34,7 @@ import static com.google.common.util.concurrent.Uninterruptibles.sleepUninterrup
import static javax.servlet.http.HttpServletResponse.SC_OK;
import static org.jclouds.Constants.PROPERTY_RELAX_HOSTNAME;
import static org.jclouds.Constants.PROPERTY_TRUST_ALL_CERTS;
import static org.jclouds.crypto.CryptoStreams.md5Base64;
import static org.jclouds.io.ByteSources.asByteSource;
import static org.jclouds.util.Strings2.toStringAndClose;
import java.io.ByteArrayInputStream;
@ -58,7 +60,6 @@ import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.ssl.SslSelectChannelConnector;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.jclouds.ContextBuilder;
import org.jclouds.io.InputSuppliers;
import org.jclouds.providers.AnonymousProviderMetadata;
import org.jclouds.rest.RestContext;
import org.testng.annotations.AfterClass;
@ -97,8 +98,7 @@ public abstract class BaseJettyTest {
this.testPort = testPort;
final InputSupplier<InputStream> oneHundredOneConstitutions = getTestDataSupplier();
md5 = md5Base64(oneHundredOneConstitutions);
md5 = base64().encode(asByteSource(oneHundredOneConstitutions.getInput()).hash(md5()).asBytes());
Handler server1Handler = new AbstractHandler() {
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
@ -183,7 +183,7 @@ public abstract class BaseJettyTest {
if (request.getHeader("Content-MD5") != null) {
String expectedMd5 = request.getHeader("Content-MD5");
String realMd5FromRequest;
realMd5FromRequest = md5Base64(InputSuppliers.of(body));
realMd5FromRequest = base64().encode(asByteSource(body).hash(md5()).asBytes());
boolean matched = expectedMd5.equals(realMd5FromRequest);
if (matched) {
response.setStatus(SC_OK);

View File

@ -19,8 +19,11 @@
package org.jclouds.http.internal;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.hash.Hashing.md5;
import static com.google.common.io.BaseEncoding.base16;
import static com.google.common.io.ByteStreams.copy;
import static java.util.concurrent.Executors.newCachedThreadPool;
import static org.jclouds.io.ByteSources.asByteSource;
import static org.testng.Assert.assertEquals;
import java.io.ByteArrayOutputStream;
@ -32,8 +35,6 @@ import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.io.InputSuppliers;
import org.jclouds.logging.Logger;
import org.testng.annotations.Test;
@ -59,9 +60,9 @@ public class WireLiveTest {
InputStream in = wire.input(fromServer);
ByteArrayOutputStream out = new ByteArrayOutputStream();// TODO
copy(in, out);
byte[] compare = CryptoStreams.md5(out.toByteArray());
byte[] compare = md5().hashBytes(out.toByteArray()).asBytes();
Thread.sleep(100);
assertEquals(CryptoStreams.hex(compare), checkNotNull(sysHttpStreamMd5, sysHttpStreamMd5));
assertEquals(base16().lowerCase().encode(compare), checkNotNull(sysHttpStreamMd5, sysHttpStreamMd5));
assertEquals(((BufferLogger) wire.getWireLog()).buff.toString().getBytes().length, 3331484);
return null;
}
@ -138,10 +139,10 @@ public class WireLiveTest {
URL url = new URL(checkNotNull(sysHttpStreamUrl, "sysHttpStreamUrl"));
URLConnection connection = url.openConnection();
HttpWire wire = setUp();
final InputStream in = wire.input(connection.getInputStream());
byte[] compare = CryptoStreams.md5(InputSuppliers.of(in));
InputStream in = wire.input(connection.getInputStream());
byte[] compare = asByteSource(in).hash(md5()).asBytes();
Thread.sleep(100);
assertEquals(CryptoStreams.hex(compare), checkNotNull(sysHttpStreamMd5, sysHttpStreamMd5));
assertEquals(base16().lowerCase().encode(compare), checkNotNull(sysHttpStreamMd5, sysHttpStreamMd5));
assertEquals(((BufferLogger) wire.getWireLog()).buff.toString().getBytes().length, 3331484);
} catch (UnknownHostException e) {
// probably in offline mode
@ -163,10 +164,10 @@ public class WireLiveTest {
URL url = new URL(checkNotNull(sysHttpStreamUrl, "sysHttpStreamUrl"));
URLConnection connection = url.openConnection();
HttpWire wire = setUpSynch();
final InputStream in = wire.input(connection.getInputStream());
byte[] compare = CryptoStreams.md5(InputSuppliers.of(in));
InputStream in = wire.input(connection.getInputStream());
byte[] compare = asByteSource(in).hash(md5()).asBytes();
Thread.sleep(100);
assertEquals(CryptoStreams.hex(compare), checkNotNull(sysHttpStreamMd5, sysHttpStreamMd5));
assertEquals(base16().lowerCase().encode(compare), checkNotNull(sysHttpStreamMd5, sysHttpStreamMd5));
assertEquals(((BufferLogger) wire.getWireLog()).buff.toString().getBytes().length, 3331484);
} catch (UnknownHostException e) {
// probably in offline mode

View File

@ -1,132 +0,0 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License 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 org.jclouds.io;
import static javax.xml.bind.DatatypeConverter.parseBase64Binary;
import static org.testng.Assert.assertEquals;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import org.jclouds.PerformanceTest;
import org.jclouds.crypto.Crypto;
import org.jclouds.crypto.CryptoStreams;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.google.inject.Guice;
import com.google.inject.Injector;
/**
* This tests the performance of Digest commands.
*
* @author Adrian Cole
*/
// NOTE:without testName, this will not call @Before* and fail w/NPE during surefire
@Test(groups = "performance", singleThreaded = true, timeOut = 2 * 60 * 1000, testName = "CryptoTest")
public class CryptoTest extends PerformanceTest {
protected Crypto crypto;
@BeforeTest
protected void createCrypto() {
Injector i = Guice.createInjector();
crypto = i.getInstance(Crypto.class);
}
public static final Object[][] base64KeyMessageDigest = {
{ parseBase64Binary("CwsLCwsLCwsLCwsLCwsLCwsLCws="), "Hi There", "thcxhlUFcmTii8C2+zeMjvFGvgA=" },
{ parseBase64Binary("SmVmZQ=="), "what do ya want for nothing?", "7/zfauXrL6LSdBbV8YTfnCWafHk=" },
{ parseBase64Binary("DAwMDAwMDAwMDAwMDAwMDAwMDAw="), "Test With Truncation", "TBoDQktV4H/n8nvh1Yu5MkqaWgQ=" },
{ parseBase64Binary("qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqo="),
"Test Using Larger Than Block-Size Key - Hash Key First", "qkrl4VJy0A6VcFY3zoo7Ve1AIRI=" },
{ parseBase64Binary("qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqo="),
"Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data",
"6OmdD0UjfXhta7qnllx4CLv/GpE=" } };
@DataProvider(name = "hmacsha1")
public Object[][] createData1() {
return base64KeyMessageDigest;
}
@Test(dataProvider = "hmacsha1")
public void testHmacSha1Base64(byte[] key, String message, String base64Digest) throws NoSuchProviderException,
NoSuchAlgorithmException, InvalidKeyException, IOException {
String b64 = CryptoStreams.macBase64(InputSuppliers.of(message), crypto.hmacSHA1(key));
assertEquals(b64, base64Digest);
}
@Test(dataProvider = "hmacsha1")
void testDigestSerialResponseTime(byte[] key, String message, String base64Digest) throws NoSuchProviderException,
NoSuchAlgorithmException, InvalidKeyException, IOException {
for (int i = 0; i < 10000; i++)
testHmacSha1Base64(key, message, base64Digest);
}
@Test(dataProvider = "hmacsha1")
void testDigestParallelResponseTime(final byte[] key, final String message, final String base64Digest)
throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeyException, InterruptedException,
ExecutionException {
CompletionService<Boolean> completer = new ExecutorCompletionService<Boolean>(exec);
for (int i = 0; i < 10000; i++)
completer.submit(new Callable<Boolean>() {
public Boolean call() throws Exception {
testHmacSha1Base64(key, message, base64Digest);
return true;
}
});
for (int i = 0; i < 10000; i++)
assert completer.take().get();
}
@DataProvider(name = "eTag")
public Object[][] createMD5Data() {
return hexMD5MessageDigest;
}
public static final Object[][] hexMD5MessageDigest = { { "apple", "1f3870be274f6c49b3e31a0c6728957f" },
{ "bear", "893b56e3cfe153fb770a120b83bac20c" }, { "candy", "c48ba993d35c3abe0380f91738fe2a34" },
{ "dogma", "95eb470e4faee302e9cd3063b1923dab" }, { "emma", "00a809937eddc44521da9521269e75c6" } };
@Test(dataProvider = "eTag")
public void testMD5Digest(String message, String hexMD5Digest) throws NoSuchProviderException,
NoSuchAlgorithmException, InvalidKeyException, IOException {
String b64 = CryptoStreams.md5Hex(InputSuppliers.of(message));
assertEquals(hexMD5Digest, b64);
}
public void testSHA1() {
crypto.sha1();
}
public void testSHA256() {
crypto.sha256();
}
public void testSHA512() {
crypto.sha512();
}
}

View File

@ -18,8 +18,10 @@
*/
package org.jclouds.rest.internal;
import static com.google.common.base.Throwables.propagate;
import static com.google.common.hash.Hashing.md5;
import static org.easymock.EasyMock.createMock;
import static org.eclipse.jetty.http.HttpHeaders.TRANSFER_ENCODING;
import static org.jclouds.io.ByteSources.asByteSource;
import static org.jclouds.rest.internal.RestAnnotationProcessor.createResponseParser;
import static org.jclouds.rest.internal.RestAnnotationProcessor.getSaxResponseParserClassOrNull;
import static org.testng.Assert.assertEquals;
@ -34,8 +36,6 @@ import java.util.concurrent.ExecutorService;
import org.jclouds.Constants;
import org.jclouds.concurrent.MoreExecutors;
import org.jclouds.concurrent.config.ConfiguresExecutorService;
import org.jclouds.crypto.Crypto;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.fallbacks.MapHttp4xxCodesToExceptions;
import org.jclouds.http.HttpCommandExecutorService;
import org.jclouds.http.HttpRequest;
@ -63,7 +63,6 @@ public abstract class BaseRestApiTest {
protected Injector injector;
protected ParseSax.Factory parserFactory;
protected Crypto crypto;
@ConfiguresHttpCommandExecutorService
@ConfiguresExecutorService
@ -118,7 +117,7 @@ public abstract class BaseRestApiTest {
Long length = Long.valueOf(payload.getBytes().length);
try {
assertContentHeadersEqual(request, contentType, contentDispositon, contentEncoding, contentLanguage,
length, contentMD5 ? CryptoStreams.md5(request.getPayload()) : null, expires);
length, contentMD5 ? asByteSource(request.getPayload().getInput()).hash(md5()).asBytes() : null, expires);
} catch (IOException e) {
propagate(e);
}

View File

@ -68,7 +68,6 @@ import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.jetty.http.HttpHeaders;
import org.jclouds.ContextBuilder;
import org.jclouds.crypto.Crypto;
import org.jclouds.date.DateService;
import org.jclouds.date.internal.SimpleDateFormatDateService;
import org.jclouds.http.HttpCommand;
@ -1775,7 +1774,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest {
public void testPutPayloadEnclosingGenerateMD5() throws SecurityException, NoSuchMethodException, IOException {
Method method = TestTransformers.class.getMethod("put", PayloadEnclosing.class);
PayloadEnclosing payloadEnclosing = new PayloadEnclosingImpl(newStringPayload("whoops"));
calculateMD5(payloadEnclosing, crypto.md5());
calculateMD5(payloadEnclosing);
HttpRequest request = factory(TestQuery.class).createRequest(method, payloadEnclosing);
assertRequestLineEquals(request, "PUT http://localhost:9999?x-ms-version=2009-07-17 HTTP/1.1");
assertNonPayloadHeadersEqual(request, "");
@ -1789,7 +1788,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest {
PayloadEnclosing payloadEnclosing = new PayloadEnclosingImpl(
newInputStreamPayload(Strings2.toInputStream("whoops")));
calculateMD5(payloadEnclosing, crypto.md5());
calculateMD5(payloadEnclosing);
HttpRequest request = factory(TestQuery.class).createRequest(method, payloadEnclosing);
assertRequestLineEquals(request, "PUT http://localhost:9999?x-ms-version=2009-07-17 HTTP/1.1");
assertNonPayloadHeadersEqual(request, "");
@ -1846,7 +1845,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest {
public void testPutPayloadWithGeneratedMD5AndNoContentType() throws SecurityException, NoSuchMethodException,
IOException {
Payload payload = newStringPayload("whoops");
calculateMD5(payload, crypto.md5());
calculateMD5(payload);
Method method = TestTransformers.class.getMethod("put", Payload.class);
HttpRequest request = factory(TestQuery.class).createRequest(method, payload);
assertRequestLineEquals(request, "PUT http://localhost:9999?x-ms-version=2009-07-17 HTTP/1.1");
@ -1867,7 +1866,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest {
public void testPutInputStreamPayloadWithMD5() throws NoSuchAlgorithmException, IOException, SecurityException,
NoSuchMethodException {
Payload payload = newStringPayload("whoops");
calculateMD5(payload, crypto.md5());
calculateMD5(payload);
Method method = TestTransformers.class.getMethod("put", Payload.class);
HttpRequest request = factory(TestQuery.class).createRequest(method, payload);
assertRequestLineEquals(request, "PUT http://localhost:9999?x-ms-version=2009-07-17 HTTP/1.1");
@ -2583,7 +2582,6 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest {
})).buildInjector();
parserFactory = injector.getInstance(ParseSax.Factory.class);
crypto = injector.getInstance(Crypto.class);
}
}

View File

@ -18,6 +18,10 @@
*/
package org.jclouds.http.apachehc;
import static com.google.common.hash.Hashing.md5;
import static com.google.common.io.BaseEncoding.base64;
import static org.jclouds.io.ByteSources.asByteSource;
import java.io.IOException;
import java.net.URI;
import java.util.concurrent.ExecutorService;
@ -30,7 +34,6 @@ import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
import org.jclouds.Constants;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.HttpUtils;
@ -71,8 +74,11 @@ public class ApacheHCHttpCommandExecutorService extends BaseHttpCommandExecutorS
@Override
protected HttpUriRequest convert(HttpRequest request) throws IOException {
HttpUriRequest returnVal = apacheHCUtils.convertToApacheRequest(request);
if (request.getPayload() != null && request.getPayload().getContentMetadata().getContentMD5() != null)
returnVal.addHeader("Content-MD5", CryptoStreams.md5Base64(request.getPayload()));
if (request.getPayload() != null && request.getPayload().getContentMetadata().getContentMD5() != null){
String md5 = base64().encode(asByteSource(request.getPayload().getInput()).hash(md5()).asBytes());
returnVal.addHeader("Content-MD5", md5);
}
return returnVal;
}

View File

@ -20,26 +20,19 @@ package org.jclouds.encryption.bouncycastle;
import org.jclouds.crypto.Crypto;
import org.jclouds.encryption.bouncycastle.config.BouncyCastleCryptoModule;
import org.jclouds.io.CryptoTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.google.inject.Guice;
import com.google.inject.Injector;
/**
* This tests the performance of Digest commands.
*
* @author Adrian Cole
*/
//NOTE:without testName, this will not call @Before* and fail w/NPE during surefire
@Test(groups = "performance", sequential = true, timeOut = 2 * 60 * 1000, testName = "BouncyCastleCryptoTest")
public class BouncyCastleCryptoTest extends CryptoTest {
@Test(groups = "unit")
public class BouncyCastleCryptoTest {
@BeforeTest
protected void createCrypto() {
Injector i = Guice.createInjector(new BouncyCastleCryptoModule());
crypto = i.getInstance(Crypto.class);
Crypto crypto = i.getInstance(Crypto.class);
assert crypto instanceof BouncyCastleCrypto;
}
}

View File

@ -18,6 +18,7 @@
*/
package org.jclouds.ssh.jsch;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
@ -25,8 +26,8 @@ import static com.google.common.base.Predicates.instanceOf;
import static com.google.common.base.Predicates.or;
import static com.google.common.base.Throwables.getCausalChain;
import static com.google.common.collect.Iterables.any;
import static org.jclouds.crypto.CryptoStreams.hex;
import static org.jclouds.crypto.CryptoStreams.md5;
import static com.google.common.hash.Hashing.md5;
import static com.google.common.io.BaseEncoding.base16;
import static org.jclouds.crypto.SshKeys.fingerprintPrivateKey;
import static org.jclouds.crypto.SshKeys.sha1PrivateKey;
@ -131,8 +132,9 @@ public class JschSshClient implements SshClient {
"you must specify a password or a key");
this.backoffLimitedRetryHandler = checkNotNull(backoffLimitedRetryHandler, "backoffLimitedRetryHandler");
if (loginCredentials.getPrivateKey() == null) {
this.toString = String.format("%s:pw[%s]@%s:%d", loginCredentials.getUser(), hex(md5(loginCredentials
.getPassword().getBytes())), host, socket.getPort());
this.toString = String.format("%s:pw[%s]@%s:%d", loginCredentials.getUser(),
base16().lowerCase().encode(md5().hashString(loginCredentials.getPassword(), UTF_8).asBytes()), host,
socket.getPort());
} else {
String fingerPrint = fingerprintPrivateKey(loginCredentials.getPrivateKey());
String sha1 = sha1PrivateKey(loginCredentials.getPrivateKey());

View File

@ -18,6 +18,7 @@
*/
package org.jclouds.sshj;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
@ -25,8 +26,8 @@ import static com.google.common.base.Predicates.instanceOf;
import static com.google.common.base.Predicates.or;
import static com.google.common.base.Throwables.getCausalChain;
import static com.google.common.collect.Iterables.any;
import static org.jclouds.crypto.CryptoStreams.hex;
import static org.jclouds.crypto.CryptoStreams.md5;
import static com.google.common.hash.Hashing.md5;
import static com.google.common.io.BaseEncoding.base16;
import static org.jclouds.crypto.SshKeys.fingerprintPrivateKey;
import static org.jclouds.crypto.SshKeys.sha1PrivateKey;
@ -150,8 +151,9 @@ public class SshjSshClient implements SshClient {
"you must specify a password or a key");
this.backoffLimitedRetryHandler = checkNotNull(backoffLimitedRetryHandler, "backoffLimitedRetryHandler");
if (loginCredentials.getPrivateKey() == null) {
this.toString = String.format("%s:pw[%s]@%s:%d", loginCredentials.getUser(), hex(md5(loginCredentials
.getPassword().getBytes())), host, socket.getPort());
this.toString = String.format("%s:pw[%s]@%s:%d", loginCredentials.getUser(),
base16().lowerCase().encode(md5().hashString(loginCredentials.getPassword(), UTF_8).asBytes()), host,
socket.getPort());
} else {
String fingerPrint = fingerprintPrivateKey(loginCredentials.getPrivateKey());
String sha1 = sha1PrivateKey(loginCredentials.getPrivateKey());

View File

@ -21,13 +21,13 @@ package org.jclouds.abiquo.http.filters;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.abiquo.config.AbiquoProperties.CREDENTIAL_IS_TOKEN;
import static org.jclouds.http.filters.BasicAuthentication.basic;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import javax.ws.rs.core.HttpHeaders;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.http.HttpException;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpRequestFilter;
@ -35,7 +35,6 @@ import org.jclouds.rest.annotations.Credential;
import org.jclouds.rest.annotations.Identity;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Charsets;
/**
* Authenticates using Basic Authentication or a generated token from previous
@ -64,18 +63,11 @@ public class AbiquoAuthentication implements HttpRequestFilter {
@Override
public HttpRequest filter(final HttpRequest request) throws HttpException {
String header = credentialIsToken ? tokenAuth(credential) : basicAuth(identity, credential);
String header = credentialIsToken ? tokenAuth(credential) : basic(identity, credential);
return request.toBuilder()
.replaceHeader(credentialIsToken ? HttpHeaders.COOKIE : HttpHeaders.AUTHORIZATION, header).build();
}
@VisibleForTesting
static String basicAuth(final String user, final String password) {
return "Basic "
+ CryptoStreams.base64(String.format("%s:%s", checkNotNull(user, "user"),
checkNotNull(password, "password")).getBytes(Charsets.UTF_8));
}
@VisibleForTesting
static String tokenAuth(final String token) {
return AUTH_TOKEN_NAME + "=" + checkNotNull(token, "token");

View File

@ -21,6 +21,7 @@ package org.jclouds.abiquo.http.filters;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.http.HttpUtils.releasePayload;
import static org.jclouds.http.filters.BasicAuthentication.basic;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
@ -140,8 +141,8 @@ public class AbiquoAuthenticationLiveApiTest {
try {
// Create a request to authenticate to the API and generate the token
HttpRequest request = HttpRequest.builder().method("GET").endpoint(URI.create(endpoint)).build();
String auth = AbiquoAuthentication.basicAuth(identity, credential);
request = request.toBuilder().replaceHeader(HttpHeaders.AUTHORIZATION, auth).build();
request = request.toBuilder().replaceHeader(HttpHeaders.AUTHORIZATION, basic(identity, credential)).build();
// Execute the request and read the generated token
HttpResponse response = context.utils().http().invoke(request);

View File

@ -19,6 +19,7 @@
package org.jclouds.abiquo.http.filters;
import static org.jclouds.http.filters.BasicAuthentication.basic;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
@ -45,7 +46,7 @@ public class AbiquoAuthenticationTest {
AbiquoAuthentication filter = new AbiquoAuthentication("identity", "credential", "false");
HttpRequest filtered = filter.filter(request);
HttpRequest expected = request.toBuilder()
.replaceHeader(HttpHeaders.AUTHORIZATION, AbiquoAuthentication.basicAuth("identity", "credential")).build();
.replaceHeader(HttpHeaders.AUTHORIZATION, basic("identity", "credential")).build();
assertFalse(filtered.getHeaders().containsKey(HttpHeaders.COOKIE));
assertEquals(filtered, expected);

View File

@ -18,7 +18,9 @@
*/
package org.jclouds.azure.management.binders;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.io.BaseEncoding.base64;
import java.util.Map;
import java.util.Map.Entry;
@ -26,7 +28,6 @@ import java.util.Map.Entry;
import javax.inject.Singleton;
import org.jclouds.azure.management.options.CreateHostedServiceOptions;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.http.HttpRequest;
import org.jclouds.rest.MapBinder;
@ -46,9 +47,8 @@ public class BindCreateHostedServiceToXmlPayload implements MapBinder {
@Override
public <R extends HttpRequest> R bindToRequest(R request, Map<String, Object> postParams) {
String serviceName = checkNotNull(postParams.get("serviceName"), "serviceName").toString();
String label = CryptoStreams.base64(checkNotNull(postParams.get("label"), "label").toString().getBytes());
String label = base64().encode(checkNotNull(postParams.get("label"), "label").toString().getBytes(UTF_8));
Optional<String> location = Optional.fromNullable((String) postParams.get("location"));
Optional<String> affinityGroup = Optional.fromNullable((String) postParams.get("affinityGroup"));

View File

@ -18,24 +18,25 @@
*/
package org.jclouds.azure.management.xml;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.io.BaseEncoding.base64;
import static org.jclouds.util.SaxUtils.currentOrNull;
import static org.jclouds.util.SaxUtils.equalsOrSuffix;
import java.net.URI;
import java.util.List;
import com.google.common.collect.Lists;
import org.jclouds.azure.management.domain.Deployment;
import org.jclouds.azure.management.domain.DeploymentSlot;
import org.jclouds.azure.management.domain.DeploymentStatus;
import org.jclouds.azure.management.domain.InstanceStatus;
import org.jclouds.azure.management.domain.RoleSize;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.http.functions.ParseSax;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import com.google.common.collect.Lists;
/**
* @see <a href="http://msdn.microsoft.com/en-us/library/ee460804" >api</a>
* @author Gérald Pereira
@ -86,7 +87,7 @@ public class DeploymentHandler extends
} else if (equalsOrSuffix(qName, "Label")) {
String label = currentOrNull(currentText);
if (label != null)
builder.deploymentLabel(new String(CryptoStreams.base64(label)));
builder.deploymentLabel(new String(base64().decode(label), UTF_8));
} else if (equalsOrSuffix(qName, "Url")) {
final String url = currentOrNull(currentText);
if (url != null)

View File

@ -18,11 +18,12 @@
*/
package org.jclouds.azure.management.xml;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.io.BaseEncoding.base64;
import static org.jclouds.util.SaxUtils.currentOrNull;
import static org.jclouds.util.SaxUtils.equalsOrSuffix;
import org.jclouds.azure.management.domain.HostedServiceProperties;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.http.functions.ParseSax;
import org.xml.sax.SAXException;
@ -60,7 +61,7 @@ public class HostedServicePropertiesHandler extends
} else if (equalsOrSuffix(qName, "AffinityGroup")) {
builder.affinityGroup(currentOrNull(currentText));
} else if (equalsOrSuffix(qName, "Label")) {
builder.label(new String(CryptoStreams.base64(currentOrNull(currentText))));
builder.label(new String(base64().decode(currentOrNull(currentText)), UTF_8));
}
currentText = new StringBuilder();
}

View File

@ -18,9 +18,10 @@
*/
package org.jclouds.snia.cdmi.v1.features;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.io.BaseEncoding.base64;
import static org.testng.Assert.assertEquals;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.snia.cdmi.v1.CDMIApi;
@ -45,7 +46,7 @@ public class ContainerApiExpectTest extends BaseCDMIApiExpectTest {
.endpoint("http://localhost:8080/MyContainer/")
.headers(ImmutableMultimap.<String, String> builder().put("X-CDMI-Specification-Version", "1.0.1")
.put("TID", "tenantId")
.put("Authorization", "Basic " + CryptoStreams.base64("username:password".getBytes()))
.put("Authorization", "Basic " + base64().encode("username:password".getBytes(UTF_8)))
.put("Accept", "application/cdmi-container").build()).build();
HttpResponse getResponse = HttpResponse.builder().statusCode(200).payload(payloadFromResource("/container.json"))

View File

@ -18,7 +18,9 @@
*/
package org.jclouds.fujitsu.fgcp.filters;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.io.BaseEncoding.base64;
import static org.jclouds.http.utils.Queries.queryParser;
import java.security.InvalidKeyException;
@ -42,7 +44,6 @@ import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import org.jclouds.Constants;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.date.TimeStamp;
import org.jclouds.fujitsu.fgcp.reference.RequestParameters;
import org.jclouds.http.HttpException;
@ -56,7 +57,6 @@ import org.jclouds.rest.annotations.ApiVersion;
import org.jclouds.rest.annotations.Credential;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Charsets;
import com.google.common.collect.Multimap;
/**
@ -171,8 +171,8 @@ public class RequestAuthenticator implements HttpRequestFilter, RequestSigner {
String signed;
try {
signer.update(stringToSign.getBytes(Charsets.UTF_8));
signed = CryptoStreams.base64(signer.sign());
signer.update(stringToSign.getBytes(UTF_8));
signed = base64().encode(signer.sign());
} catch (SignatureException e) {
throw new HttpException("error signing request", e);
}
@ -190,7 +190,7 @@ public class RequestAuthenticator implements HttpRequestFilter, RequestSigner {
String signatureData = String.format("%s&%s&%s&%s", timezone, expires,
signatureVersion, signatureMethod);
String accessKeyId = CryptoStreams.base64(signatureData.getBytes(Charsets.UTF_8));
String accessKeyId = base64().encode(signatureData.getBytes(UTF_8));
return accessKeyId.replace("\n", "\r\n");
}

View File

@ -18,10 +18,14 @@
*/
package org.jclouds.googlecompute.internal;
import com.google.common.base.Joiner;
import com.google.common.base.Ticker;
import com.google.inject.Binder;
import com.google.inject.Module;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.io.BaseEncoding.base64Url;
import java.net.URI;
import java.util.Properties;
import javax.ws.rs.core.MediaType;
import org.jclouds.collect.PagedIterable;
import org.jclouds.collect.PagedIterables;
import org.jclouds.googlecompute.domain.ListPage;
@ -30,12 +34,10 @@ import org.jclouds.http.HttpResponse;
import org.jclouds.oauth.v2.OAuthConstants;
import org.jclouds.rest.internal.BaseRestApiExpectTest;
import javax.ws.rs.core.MediaType;
import java.net.URI;
import java.nio.charset.Charset;
import java.util.Properties;
import static org.jclouds.crypto.CryptoStreams.base64Url;
import com.google.common.base.Joiner;
import com.google.common.base.Ticker;
import com.google.inject.Binder;
import com.google.inject.Module;
/**
* @author Adrian Cole
@ -93,9 +95,9 @@ public class BaseGoogleComputeExpectTest<T> extends BaseRestApiExpectTest<T> {
String payload = "grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&" +
// Base64 Encoded Header
"assertion=" + base64Url(header.getBytes(Charset.forName("UTF-8"))) + "." +
"assertion=" + base64Url().omitPadding().encode(header.getBytes(UTF_8)) + "." +
// Base64 Encoded Claims
base64Url(claims.getBytes(Charset.forName("UTF-8"))) + ".";
base64Url().omitPadding().encode(claims.getBytes(UTF_8)) + ".";
return HttpRequest.builder()
.method("POST")

View File

@ -18,22 +18,24 @@
*/
package org.jclouds.oauth.v2.json;
import com.google.common.base.Charsets;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import org.jclouds.crypto.CryptoStreams;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.base.Joiner.on;
import static com.google.common.io.BaseEncoding.base64Url;
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.http.HttpRequest;
import org.jclouds.io.Payloads;
import org.jclouds.json.Json;
import org.jclouds.oauth.v2.domain.TokenRequest;
import org.jclouds.oauth.v2.domain.TokenRequestFormat;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.Set;
import static com.google.common.base.Joiner.on;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
/**
* Formats a token request into JWT format namely:
@ -66,18 +68,14 @@ public class JWTTokenRequestFormat implements TokenRequestFormat {
public <R extends HttpRequest> R formatRequest(R httpRequest, TokenRequest tokenRequest) {
HttpRequest.Builder builder = httpRequest.toBuilder();
// transform to json and encode in base 64
// use commons-codec base 64 which properly encodes urls (jclouds's Base64 does not)
String encodedHeader = json.toJson(tokenRequest.getHeader());
String encodedClaimSet = json.toJson(tokenRequest.getClaimSet());
String encodedSignature = null;
encodedHeader = base64Url().omitPadding().encode(encodedHeader.getBytes(UTF_8));
encodedClaimSet = base64Url().omitPadding().encode(encodedClaimSet.getBytes(UTF_8));
encodedHeader = CryptoStreams.base64Url(encodedHeader.getBytes(Charsets.UTF_8));
encodedClaimSet = CryptoStreams.base64Url(encodedClaimSet.getBytes(Charsets.UTF_8));
byte[] signature = signer.apply(on(".").join(encodedHeader, encodedClaimSet).getBytes(Charsets.UTF_8));
encodedSignature = signature != null ? CryptoStreams.base64Url(signature) : "";
byte[] signature = signer.apply(on(".").join(encodedHeader, encodedClaimSet).getBytes(UTF_8));
String encodedSignature = signature != null ? base64Url().omitPadding().encode(signature) : "";
// the final assertion in base 64 encoded {header}.{claimSet}.{signature} format
String assertion = on(".").join(encodedHeader, encodedClaimSet, encodedSignature);

View File

@ -18,6 +18,15 @@
*/
package org.jclouds.oauth.v2.features;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.io.BaseEncoding.base64Url;
import static org.testng.Assert.assertEquals;
import java.net.URI;
import java.util.Properties;
import javax.ws.rs.core.MediaType;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.oauth.v2.OAuthApi;
@ -29,14 +38,6 @@ import org.jclouds.oauth.v2.domain.TokenRequest;
import org.jclouds.oauth.v2.internal.BaseOAuthApiExpectTest;
import org.testng.annotations.Test;
import javax.ws.rs.core.MediaType;
import java.net.URI;
import java.nio.charset.Charset;
import java.util.Properties;
import static org.jclouds.crypto.CryptoStreams.base64Url;
import static org.testng.Assert.assertEquals;
/**
* Tests that a token requess is well formed.
*
@ -68,9 +69,9 @@ public class OAuthApiExpectTest extends BaseOAuthApiExpectTest {
private static final String URL_ENCODED_TOKEN_REQUEST =
"grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&" +
// Base64 Encoded Header
"assertion=" + base64Url(header.getBytes(Charset.forName("UTF-8"))) + "." +
"assertion=" + base64Url().omitPadding().encode(header.getBytes(UTF_8)) + "." +
// Base64 Encoded Claims
base64Url(claims.getBytes(Charset.forName("UTF-8"))) + "." +
base64Url().omitPadding().encode(claims.getBytes(UTF_8)) + "." +
// Base64 encoded {header}.{claims} signature (using SHA256)
"W2Lesr_98AzVYiMbzxFqmwcOjpIWlwqkC6pNn1fXND9oSDNNnFhy-AAR6DKH-x9ZmxbY80" +
"R5fH-OCeWumXlVgceKN8Z2SmgQsu8ElTpypQA54j_5j8vUImJ5hsOUYPeyF1U2BUzZ3L5g" +

View File

@ -17,9 +17,11 @@
* under the License.
*/
package org.jclouds.oauth.v2.functions;
import org.jclouds.crypto.CryptoStreams;
import org.testng.annotations.Test;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.base.Suppliers.ofInstance;
import static com.google.common.io.BaseEncoding.base64Url;
import static org.testng.Assert.assertNotNull;
import static org.testng.AssertJUnit.assertEquals;
import java.io.IOException;
import java.security.InvalidKeyException;
@ -27,10 +29,7 @@ import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.spec.InvalidKeySpecException;
import static com.google.common.base.Suppliers.ofInstance;
import static org.testng.Assert.assertNotNull;
import static org.testng.AssertJUnit.assertEquals;
import org.testng.annotations.Test;
/**
* Tests the SignOrProduceMacForToken
@ -58,8 +57,9 @@ public class SignerFunctionTest {
ofInstance(OAuthCredentialsFromPKTest
.loadOAuthCredentials()));
signer.loadSignatureOrMacOrNone();
byte[] payloadSignature = signer.apply(PAYLOAD.getBytes("UTF-8"));
byte[] payloadSignature = signer.apply(PAYLOAD.getBytes(UTF_8));
assertNotNull(payloadSignature);
assertEquals(CryptoStreams.base64Url(payloadSignature), SHA256withRSA_PAYLOAD_SIGNATURE_RESULT);
assertEquals(base64Url().omitPadding().encode(payloadSignature), SHA256withRSA_PAYLOAD_SIGNATURE_RESULT);
}
}

View File

@ -19,36 +19,25 @@
package org.jclouds.oauth.v2.internal;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.io.BaseEncoding.base64Url;
import static org.testng.Assert.assertTrue;
import org.jclouds.crypto.CryptoStreams;
import org.testng.annotations.Test;
import com.google.common.base.Charsets;
/**
* Tests that the Base64 implementations used to Base64 encode the tokens are Url safe.
*
*
* @author David Alves
*/
@Test(groups = "unit")
public class Base64UrlSafeTest {
public static final String STRING_THAT_GENERATES_URL_UNSAFE_BASE64_ENCODING = "§1234567890'+±!\"#$%&/()" +
"=?*qwertyuiopº´WERTYUIOPªàsdfghjklç~ASDFGHJKLÇ^<zxcvbnm," +
".->ZXCVBNM;:_@€";
public void testJcloudsCoreBase64IsNotUrlSafe() {
String encoded = new String(CryptoStreams.base64(STRING_THAT_GENERATES_URL_UNSAFE_BASE64_ENCODING
.getBytes(Charsets.UTF_8)));
assertTrue(encoded.contains("+"), encoded);
assertTrue(encoded.contains("/"), encoded);
}
public static final String STRING_THAT_GENERATES_URL_UNSAFE_BASE64_ENCODING = "§1234567890'+±!\"#$%&/()"
+ "=?*qwertyuiopº´WERTYUIOPªàsdfghjklç~ASDFGHJKLÇ^<zxcvbnm,.->ZXCVBNM;:_@€";
public void testUsedBase64IsUrlSafe() {
String encoded = CryptoStreams.base64Url(
STRING_THAT_GENERATES_URL_UNSAFE_BASE64_ENCODING.getBytes(Charsets.UTF_8));
String encoded = base64Url().omitPadding().encode(STRING_THAT_GENERATES_URL_UNSAFE_BASE64_ENCODING.getBytes(UTF_8));
assertTrue(!encoded.contains("+"));
assertTrue(!encoded.contains("/"));
assertTrue(!encoded.endsWith("="));

Some files were not shown because too many files have changed in this diff Show More