15
15
*/
16
16
17
17
package com .google .cdn ;
18
- import java .io .UnsupportedEncodingException ;
19
- import java .net .URLEncoder ;
20
- import java .security .InvalidKeyException ;
21
- import java .security .NoSuchAlgorithmException ;
22
- import java .util .Base64 ;
23
18
24
- import javax .crypto .Mac ;
25
- import javax .crypto .spec .SecretKeySpec ;
26
19
import java .nio .file .Files ;
27
20
import java .nio .file .Paths ;
21
+ import java .security .InvalidKeyException ;
28
22
import java .security .Key ;
23
+ import java .security .NoSuchAlgorithmException ;
24
+ import java .util .Base64 ;
29
25
import java .util .Calendar ;
30
26
import java .util .Date ;
27
+ import javax .crypto .Mac ;
28
+ import javax .crypto .spec .SecretKeySpec ;
31
29
30
+ /**
31
+ * Samples to create a signed URL for a Cloud CDN endpoint
32
+ */
32
33
public class SignedUrls {
33
34
34
35
// [START signUrl]
@@ -52,7 +53,7 @@ public class SignedUrls {
52
53
* @param expirationTime the date that the signed URL expires
53
54
* @return a properly formatted signed URL
54
55
* @throws InvalidKeyException when there is an error generating the signature for the input key
55
- * @throws NoSuchAlgorithmException when the HmacSHA1 algorithm is not available in the environment
56
+ * @throws NoSuchAlgorithmException when HmacSHA1 algorithm is not available in the environment
56
57
*/
57
58
public static String signUrl (String url ,
58
59
byte [] key ,
@@ -62,16 +63,18 @@ public static String signUrl(String url,
62
63
63
64
final long unixTime = expirationTime .getTime () / 1000 ;
64
65
65
- String urlToSign = url +
66
- (url .contains ("?" ) ? "&" : "?" ) +
67
- "Expires=" + unixTime +
68
- "&KeyName=" + keyName ;
66
+ String urlToSign = url
67
+ + (url .contains ("?" ) ? "&" : "?" )
68
+ + "Expires=" + unixTime
69
+ + "&KeyName=" + keyName ;
69
70
70
71
String encoded = SignedUrls .getSignature (key , urlToSign );
71
- return urlToSign + "&Signature=" + encoded ;
72
+ return urlToSign + "&Signature=" + encoded ;
72
73
}
73
74
74
- public static String getSignature (byte [] privateKey , String input ) throws InvalidKeyException , NoSuchAlgorithmException {
75
+ public static String getSignature (byte [] privateKey , String input )
76
+ throws InvalidKeyException , NoSuchAlgorithmException {
77
+
75
78
final String algorithm = "HmacSHA1" ;
76
79
Key key = new SecretKeySpec (privateKey , 0 , privateKey .length , algorithm );
77
80
Mac mac = Mac .getInstance (algorithm );
@@ -87,7 +90,8 @@ public static void main(String[] args) throws Exception {
87
90
Date tomorrow = cal .getTime ();
88
91
89
92
//read the key as a base 64 url-safe encoded string
90
- String base64String = new String (Files .readAllBytes (Paths .get ("./src/main/resources/my-key" )));
93
+ final String keyPath = "./src/main/resources/my-key" ;
94
+ String base64String = new String (Files .readAllBytes (Paths .get (keyPath )));
91
95
//turn the key string into a byte array
92
96
byte [] keyBytes = Base64 .getUrlDecoder ().decode (base64String );
93
97
0 commit comments