Skip to content

Commit

Permalink
fixed indents
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-sanche committed Feb 8, 2018
1 parent 481a26e commit de2a3f0
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 87 deletions.
116 changes: 58 additions & 58 deletions cdn/signed-urls/src/main/java/com/google/cdn/SignedUrls.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,70 +32,70 @@
*/
public class SignedUrls {

// [START signUrl]
/**
* Creates a signed URL for a Cloud CDN endpoint with the given key
* should pass in a properly formatted URL
* will work:
* https://www.google.com/
* http://www.google.com/
* https://www.google.com/?foo=test&bar=test
* https://www.google.com/foo
*
* won't work:
* https://www.google.com
* https://www.google.com?test
* www.google.com
*
* @param url the Cloud CDN endpoint to sign
* @param key encoded as a 16-byte array
* @param keyName the name of the signing key added to the back end bucket or service
* @param expirationTime the date that the signed URL expires
* @return a properly formatted signed URL
* @throws InvalidKeyException when there is an error generating the signature for the input key
* @throws NoSuchAlgorithmException when HmacSHA1 algorithm is not available in the environment
*/
public static String signUrl(String url,
byte[] key,
String keyName,
Date expirationTime)
throws InvalidKeyException, NoSuchAlgorithmException {
// [START signUrl]
/**
* Creates a signed URL for a Cloud CDN endpoint with the given key
* should pass in a properly formatted URL
* will work:
* https://www.google.com/
* http://www.google.com/
* https://www.google.com/?foo=test&bar=test
* https://www.google.com/foo
*
* won't work:
* https://www.google.com
* https://www.google.com?test
* www.google.com
*
* @param url the Cloud CDN endpoint to sign
* @param key encoded as a 16-byte array
* @param keyName the name of the signing key added to the back end bucket or service
* @param expirationTime the date that the signed URL expires
* @return a properly formatted signed URL
* @throws InvalidKeyException when there is an error generating the signature for the input key
* @throws NoSuchAlgorithmException when HmacSHA1 algorithm is not available in the environment
*/
public static String signUrl(String url,
byte[] key,
String keyName,
Date expirationTime)
throws InvalidKeyException, NoSuchAlgorithmException {

final long unixTime = expirationTime.getTime() / 1000;
final long unixTime = expirationTime.getTime() / 1000;

String urlToSign = url
+ (url.contains("?") ? "&" : "?")
+ "Expires=" + unixTime
+ "&KeyName=" + keyName;
String urlToSign = url
+ (url.contains("?") ? "&" : "?")
+ "Expires=" + unixTime
+ "&KeyName=" + keyName;

String encoded = SignedUrls.getSignature(key, urlToSign);
return urlToSign + "&Signature=" + encoded;
}
String encoded = SignedUrls.getSignature(key, urlToSign);
return urlToSign + "&Signature=" + encoded;
}

public static String getSignature(byte[] privateKey, String input)
throws InvalidKeyException, NoSuchAlgorithmException {
public static String getSignature(byte[] privateKey, String input)
throws InvalidKeyException, NoSuchAlgorithmException {

final String algorithm = "HmacSHA1";
Key key = new SecretKeySpec(privateKey, 0, privateKey.length, algorithm);
Mac mac = Mac.getInstance(algorithm);
mac.init(key);
return Base64.getUrlEncoder().encodeToString(mac.doFinal(input.getBytes()));
}
// [END signUrl]
final String algorithm = "HmacSHA1";
Key key = new SecretKeySpec(privateKey, 0, privateKey.length, algorithm);
Mac mac = Mac.getInstance(algorithm);
mac.init(key);
return Base64.getUrlEncoder().encodeToString(mac.doFinal(input.getBytes()));
}
// [END signUrl]

public static void main(String[] args) throws Exception {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.DATE, 1);
Date tomorrow = cal.getTime();
public static void main(String[] args) throws Exception {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.DATE, 1);
Date tomorrow = cal.getTime();

//read the key as a base 64 url-safe encoded string
final String keyPath = "./src/main/resources/my-key";
String base64String = new String(Files.readAllBytes(Paths.get(keyPath)));
//turn the key string into a byte array
byte[] keyBytes = Base64.getUrlDecoder().decode(base64String);
//read the key as a base 64 url-safe encoded string
final String keyPath = "./src/main/resources/my-key";
String base64String = new String(Files.readAllBytes(Paths.get(keyPath)));
//turn the key string into a byte array
byte[] keyBytes = Base64.getUrlDecoder().decode(base64String);

String result = signUrl("http://35.186.234.33/index.html", keyBytes, "my-key", tomorrow);
System.out.println(result);
}
String result = signUrl("http://35.186.234.33/index.html", keyBytes, "my-key", tomorrow);
System.out.println(result);
}
}
56 changes: 27 additions & 29 deletions cdn/signed-urls/src/test/java/com/google/cdn/SignedUrlsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,31 @@
@RunWith(JUnit4.class)
@SuppressWarnings("checkstyle:abbreviationaswordinname")
public class SignedUrlsTest {
private static long TIMESTAMP = 1518135754;
private static Date EXPIRATION = new Date(TIMESTAMP * 1000);
private static byte[] KEY_BYTES = Base64.getUrlDecoder().decode("aaaaaaaaaaaaaaaaaaaaaa==");
private static String KEY_NAME = "my-key";
private static String BASE_URL = "https://www.google.com/";

@Test
public void testUrlPath() throws Exception {
String result = signUrl(BASE_URL + "foo", KEY_BYTES, KEY_NAME, EXPIRATION);
final String expected = "https://www.google.com/foo?Expires=1518135754&KeyName=my-key&Signature=SBdQtypBTcz0gvHRDZjy2pc-F0s=";
assertEquals(result, expected);
}

@Test
public void testUrlParams() throws Exception {
String result = signUrl(BASE_URL + "?param=true", KEY_BYTES, KEY_NAME, EXPIRATION);
final String expected = "https://www.google.com/?param=true&Expires=1518135754&KeyName=my-key&Signature=ilkstIAKFvOlckbVdfZBWAror3o=";
assertEquals(result, expected);
}


@Test
public void testStandard() throws Exception {
String result = signUrl(BASE_URL, KEY_BYTES, KEY_NAME, EXPIRATION);
final String expected = "https://www.google.com/?Expires=1518135754&KeyName=my-key&Signature=yYnIFLMqsuGfpSuo7nf7wk21boM=";
assertEquals(result, expected);
}


private static long TIMESTAMP = 1518135754;
private static Date EXPIRATION = new Date(TIMESTAMP * 1000);
private static byte[] KEY_BYTES = Base64.getUrlDecoder().decode("aaaaaaaaaaaaaaaaaaaaaa==");
private static String KEY_NAME = "my-key";
private static String BASE_URL = "https://www.google.com/";

@Test
public void testUrlPath() throws Exception {
String result = signUrl(BASE_URL + "foo", KEY_BYTES, KEY_NAME, EXPIRATION);
final String expected = "https://www.google.com/foo?Expires=1518135754&KeyName=my-key&Signature=SBdQtypBTcz0gvHRDZjy2pc-F0s=";
assertEquals(result, expected);
}

@Test
public void testUrlParams() throws Exception {
String result = signUrl(BASE_URL + "?param=true", KEY_BYTES, KEY_NAME, EXPIRATION);
final String expected = "https://www.google.com/?param=true&Expires=1518135754&KeyName=my-key&Signature=ilkstIAKFvOlckbVdfZBWAror3o=";
assertEquals(result, expected);
}


@Test
public void testStandard() throws Exception {
String result = signUrl(BASE_URL, KEY_BYTES, KEY_NAME, EXPIRATION);
final String expected = "https://www.google.com/?Expires=1518135754&KeyName=my-key&Signature=yYnIFLMqsuGfpSuo7nf7wk21boM=";
assertEquals(result, expected);
}
}

0 comments on commit de2a3f0

Please sign in to comment.