Skip to content

Commit

Permalink
Cleaned up Hex and Digest code
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbodart committed Apr 17, 2016
1 parent d538c53 commit c052792
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 103 deletions.
2 changes: 1 addition & 1 deletion build/runtime.dependencies
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
mvn://repo.bodar.com/com.googlecode.totallylazy:totallylazy:pack|sources:2.241
mvn://repo.bodar.com/com.googlecode.totallylazy:totallylazy:pack|sources:2.249
mvn://repo.bodar.com/com.googlecode.yadic:yadic:pack|sources:2.45
6 changes: 3 additions & 3 deletions src/com/googlecode/utterlyidle/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ public Object value() {
}

public String toString() {
return string(asBytes());
return string(toBytes());
}

public byte[] asBytes() {
public byte[] toBytes() {
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
writer.apply(outputStream);
byte[] bytes = outputStream.toByteArray();
Expand Down Expand Up @@ -87,7 +87,7 @@ protected InputStream get() throws Exception {

public Option<Integer> length() {
if(isStreaming()) return none();
return some(asBytes().length);
return some(toBytes().length);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private String canonicalQueryString(Request request) {
}

private String payloadHash(Request request) {
return AwsHmacSha256.hash(request.entity().asBytes());
return AwsHmacSha256.hash(request.entity().toBytes());
}

public String signedHeaders() {
Expand Down
33 changes: 14 additions & 19 deletions src/com/googlecode/utterlyidle/aws/AwsHmacSha256.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,37 @@
package com.googlecode.utterlyidle.aws;

import com.googlecode.totallylazy.security.Digest;
import com.googlecode.totallylazy.security.Hex;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class AwsHmacSha256 {
import static com.googlecode.totallylazy.Bytes.bytes;

public static String hash(String payload) {
return hash(payload.getBytes());
public interface AwsHmacSha256 {
static String hash(String payload) {
return hash(bytes(payload));
}

public static String hash(byte[] payload) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] res = digest.digest(payload);
return hex(res);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
static String hash(byte[] payload) {
return Digest.sha256(payload).toHex();
}

public static byte[] hmacSHA256(byte[] key, String data) {
static byte[] hmacSHA256(byte[] key, String data) {
try {
String algorithm = "HmacSHA256";
Mac mac = Mac.getInstance(algorithm);
mac.init(new SecretKeySpec(key, algorithm));
return mac.doFinal(data.getBytes("UTF8"));
return mac.doFinal(bytes(data));
} catch (Exception e) {
throw new RuntimeException("Could not run HMAC SHA256", e);
}
}

public static String hex(byte[] data) {
StringBuilder result = new StringBuilder();
for (byte aByte : data) {
result.append(String.format("%02x", aByte));
}
return result.toString().toLowerCase();
static String hex(byte[] data) {
return Hex.encode(data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import java.util.List;

import static com.googlecode.totallylazy.Closeables.using;
import static com.googlecode.totallylazy.LazyException.lazyException;
Expand Down Expand Up @@ -141,7 +140,7 @@ private Response defaultHandle(final Request request, final URLConnection connec

private Response putFile(Request request) throws IOException {
File file = request.uri().toFile();
Files.write(request.entity().asBytes(), file);
Files.write(request.entity().toBytes(), file);
for (String date : request.headers().valueOption(LAST_MODIFIED)) file.setLastModified(Dates.parse(date).getTime());
return Response.created(request.uri());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static Response setContentLength(Response response) {

public static HeaderParameters setContentLength(Entity entity, HeaderParameters headers) {
if(headers.contains(CONTENT_LENGTH)) {
byte[] bytes = entity.asBytes();
byte[] bytes = entity.toBytes();
headers.replace(CONTENT_LENGTH, String.valueOf(bytes.length));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.googlecode.utterlyidle.modules;
package com.googlecode.utterlyidle.handlers;

import com.googlecode.totallylazy.functions.Function2;
import com.googlecode.utterlyidle.HttpHandler;
import com.googlecode.utterlyidle.Request;
import com.googlecode.utterlyidle.Response;
import com.googlecode.utterlyidle.Status;
import com.googlecode.totallylazy.security.Digest;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
Expand All @@ -21,7 +22,7 @@
import static com.googlecode.utterlyidle.HttpHeaders.SET_COOKIE;
import static com.googlecode.utterlyidle.Status.NOT_MODIFIED;
import static com.googlecode.utterlyidle.annotations.HttpMethod.GET;
import static com.googlecode.utterlyidle.modules.Digest.md5;
import static com.googlecode.totallylazy.security.Digest.md5;

public class EtagHandler implements HttpHandler {
public static final List<String> safeHeaders = new CopyOnWriteArrayList<String>(list(DATE, LAST_MODIFIED, CACHE_CONTROL, EXPIRES, SET_COOKIE));
Expand All @@ -38,12 +39,12 @@ public Response handle(Request request) throws Exception {
return response;
}

Digest digest = md5(response.entity().asBytes());
Digest digest = md5(response.entity().toBytes());
String etag = strongEtag(digest);
if (etag.equals(request.headers().getValue(IF_NONE_MATCH))) {
return copySafeHeaders(response, Response.response(NOT_MODIFIED));
}
return response.header(ETAG, etag).header(Content_MD5, digest.asBase64());
return response.header(ETAG, etag).header(Content_MD5, digest.toBase64());
}

private Response copySafeHeaders(final Response source, Response destination) {
Expand All @@ -55,7 +56,7 @@ private static Function2<Response, String, Response> copyFrom(final Response sou
}

private String strongEtag(Digest digest) {
return "\"" + digest.asHex() + "\"";
return "\"" + digest.toHex() + "\"";
}

}
2 changes: 1 addition & 1 deletion src/com/googlecode/utterlyidle/handlers/GzipHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public Response handle(Request request) throws Exception {
!marker.isInternal(request) &&
response.entity().length().is(greaterThan(0)) &&
gZipPolicy.matches(pair(request, response))) {
return result.header(CONTENT_ENCODING, GZIP).entity(GZip.gzip(response.entity().asBytes()));
return result.header(CONTENT_ENCODING, GZIP).entity(GZip.gzip(response.entity().toBytes()));
}
return result;
}
Expand Down
64 changes: 0 additions & 64 deletions src/com/googlecode/utterlyidle/modules/Digest.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import com.googlecode.utterlyidle.HttpHandler;
import com.googlecode.utterlyidle.MediaType;
import com.googlecode.utterlyidle.handlers.CacheControlHandler;
import com.googlecode.utterlyidle.handlers.CachePolicy;
import com.googlecode.utterlyidle.handlers.GZipPolicy;
import com.googlecode.utterlyidle.handlers.GzipHandler;
import com.googlecode.utterlyidle.handlers.*;
import com.googlecode.yadic.Container;

import static com.googlecode.utterlyidle.handlers.CachePolicy.cachePolicy;
Expand Down
1 change: 0 additions & 1 deletion test/com/googlecode/utterlyidle/aws/AwsHmacSha256Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import static org.junit.Assert.assertThat;

public class AwsHmacSha256Test {

@Test
public void hash_content() {
assertThat(hash("test string"), is("d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.googlecode.utterlyidle.modules;

import com.googlecode.utterlyidle.*;
import com.googlecode.utterlyidle.handlers.EtagHandler;
import org.junit.Test;

import static com.googlecode.utterlyidle.HttpHeaders.*;
import static com.googlecode.utterlyidle.Response.ok;
import static com.googlecode.utterlyidle.Response.seeOther;
import static com.googlecode.utterlyidle.Status.OK;
import static com.googlecode.utterlyidle.handlers.ReturnResponseHandler.returnsResponse;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
Expand Down

0 comments on commit c052792

Please sign in to comment.