|
32 | 32 | */ |
33 | 33 | public class SignedUrls { |
34 | 34 |
|
35 | | - // [START signUrl] |
36 | | - /** |
37 | | - * Creates a signed URL for a Cloud CDN endpoint with the given key |
38 | | - * should pass in a properly formatted URL |
39 | | - * will work: |
40 | | - * https://www.google.com/ |
41 | | - * http://www.google.com/ |
42 | | - * https://www.google.com/?foo=test&bar=test |
43 | | - * https://www.google.com/foo |
44 | | - * |
45 | | - * won't work: |
46 | | - * https://www.google.com |
47 | | - * https://www.google.com?test |
48 | | - * www.google.com |
49 | | - * |
50 | | - * @param url the Cloud CDN endpoint to sign |
51 | | - * @param key encoded as a 16-byte array |
52 | | - * @param keyName the name of the signing key added to the back end bucket or service |
53 | | - * @param expirationTime the date that the signed URL expires |
54 | | - * @return a properly formatted signed URL |
55 | | - * @throws InvalidKeyException when there is an error generating the signature for the input key |
56 | | - * @throws NoSuchAlgorithmException when HmacSHA1 algorithm is not available in the environment |
57 | | - */ |
58 | | - public static String signUrl(String url, |
59 | | - byte[] key, |
60 | | - String keyName, |
61 | | - Date expirationTime) |
62 | | - throws InvalidKeyException, NoSuchAlgorithmException { |
| 35 | + // [START signUrl] |
| 36 | + /** |
| 37 | + * Creates a signed URL for a Cloud CDN endpoint with the given key |
| 38 | + * should pass in a properly formatted URL |
| 39 | + * will work: |
| 40 | + * https://www.google.com/ |
| 41 | + * http://www.google.com/ |
| 42 | + * https://www.google.com/?foo=test&bar=test |
| 43 | + * https://www.google.com/foo |
| 44 | + * |
| 45 | + * won't work: |
| 46 | + * https://www.google.com |
| 47 | + * https://www.google.com?test |
| 48 | + * www.google.com |
| 49 | + * |
| 50 | + * @param url the Cloud CDN endpoint to sign |
| 51 | + * @param key encoded as a 16-byte array |
| 52 | + * @param keyName the name of the signing key added to the back end bucket or service |
| 53 | + * @param expirationTime the date that the signed URL expires |
| 54 | + * @return a properly formatted signed URL |
| 55 | + * @throws InvalidKeyException when there is an error generating the signature for the input key |
| 56 | + * @throws NoSuchAlgorithmException when HmacSHA1 algorithm is not available in the environment |
| 57 | + */ |
| 58 | + public static String signUrl(String url, |
| 59 | + byte[] key, |
| 60 | + String keyName, |
| 61 | + Date expirationTime) |
| 62 | + throws InvalidKeyException, NoSuchAlgorithmException { |
63 | 63 |
|
64 | | - final long unixTime = expirationTime.getTime() / 1000; |
| 64 | + final long unixTime = expirationTime.getTime() / 1000; |
65 | 65 |
|
66 | | - String urlToSign = url |
67 | | - + (url.contains("?") ? "&" : "?") |
68 | | - + "Expires=" + unixTime |
69 | | - + "&KeyName=" + keyName; |
| 66 | + String urlToSign = url |
| 67 | + + (url.contains("?") ? "&" : "?") |
| 68 | + + "Expires=" + unixTime |
| 69 | + + "&KeyName=" + keyName; |
70 | 70 |
|
71 | | - String encoded = SignedUrls.getSignature(key, urlToSign); |
72 | | - return urlToSign + "&Signature=" + encoded; |
73 | | - } |
| 71 | + String encoded = SignedUrls.getSignature(key, urlToSign); |
| 72 | + return urlToSign + "&Signature=" + encoded; |
| 73 | + } |
74 | 74 |
|
75 | | - public static String getSignature(byte[] privateKey, String input) |
76 | | - throws InvalidKeyException, NoSuchAlgorithmException { |
| 75 | + public static String getSignature(byte[] privateKey, String input) |
| 76 | + throws InvalidKeyException, NoSuchAlgorithmException { |
77 | 77 |
|
78 | | - final String algorithm = "HmacSHA1"; |
79 | | - Key key = new SecretKeySpec(privateKey, 0, privateKey.length, algorithm); |
80 | | - Mac mac = Mac.getInstance(algorithm); |
81 | | - mac.init(key); |
82 | | - return Base64.getUrlEncoder().encodeToString(mac.doFinal(input.getBytes())); |
83 | | - } |
84 | | - // [END signUrl] |
| 78 | + final String algorithm = "HmacSHA1"; |
| 79 | + Key key = new SecretKeySpec(privateKey, 0, privateKey.length, algorithm); |
| 80 | + Mac mac = Mac.getInstance(algorithm); |
| 81 | + mac.init(key); |
| 82 | + return Base64.getUrlEncoder().encodeToString(mac.doFinal(input.getBytes())); |
| 83 | + } |
| 84 | + // [END signUrl] |
85 | 85 |
|
86 | | - public static void main(String[] args) throws Exception { |
87 | | - Calendar cal = Calendar.getInstance(); |
88 | | - cal.setTime(new Date()); |
89 | | - cal.add(Calendar.DATE, 1); |
90 | | - Date tomorrow = cal.getTime(); |
| 86 | + public static void main(String[] args) throws Exception { |
| 87 | + Calendar cal = Calendar.getInstance(); |
| 88 | + cal.setTime(new Date()); |
| 89 | + cal.add(Calendar.DATE, 1); |
| 90 | + Date tomorrow = cal.getTime(); |
91 | 91 |
|
92 | | - //read the key as a base 64 url-safe encoded string |
93 | | - final String keyPath = "./src/main/resources/my-key"; |
94 | | - String base64String = new String(Files.readAllBytes(Paths.get(keyPath))); |
95 | | - //turn the key string into a byte array |
96 | | - byte[] keyBytes = Base64.getUrlDecoder().decode(base64String); |
| 92 | + //read the key as a base 64 url-safe encoded string |
| 93 | + final String keyPath = "./src/main/resources/my-key"; |
| 94 | + String base64String = new String(Files.readAllBytes(Paths.get(keyPath))); |
| 95 | + //turn the key string into a byte array |
| 96 | + byte[] keyBytes = Base64.getUrlDecoder().decode(base64String); |
97 | 97 |
|
98 | | - String result = signUrl("http://35.186.234.33/index.html", keyBytes, "my-key", tomorrow); |
99 | | - System.out.println(result); |
100 | | - } |
| 98 | + String result = signUrl("http://35.186.234.33/index.html", keyBytes, "my-key", tomorrow); |
| 99 | + System.out.println(result); |
| 100 | + } |
101 | 101 | } |
0 commit comments