|
| 1 | +/* |
| 2 | + * Copyright 2018 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cdn; |
| 18 | + |
| 19 | +import java.nio.file.Files; |
| 20 | +import java.nio.file.Paths; |
| 21 | +import java.security.InvalidKeyException; |
| 22 | +import java.security.Key; |
| 23 | +import java.security.NoSuchAlgorithmException; |
| 24 | +import java.util.Base64; |
| 25 | +import java.util.Calendar; |
| 26 | +import java.util.Date; |
| 27 | +import javax.crypto.Mac; |
| 28 | +import javax.crypto.spec.SecretKeySpec; |
| 29 | + |
| 30 | +/** |
| 31 | + * Samples to create a signed URL for a Cloud CDN endpoint |
| 32 | + */ |
| 33 | +public class SignedUrls { |
| 34 | + |
| 35 | + // [START signUrl] |
| 36 | + /** |
| 37 | + * Creates a signed URL for a Cloud CDN endpoint with the given key |
| 38 | + * URL must start with http:// or https://, and must contain a forward |
| 39 | + * slash (/) after the hostname. |
| 40 | + * @param url the Cloud CDN endpoint to sign |
| 41 | + * @param key url signing key uploaded to the backend service/bucket, as a 16-byte array |
| 42 | + * @param keyName the name of the signing key added to the back end bucket or service |
| 43 | + * @param expirationTime the date that the signed URL expires |
| 44 | + * @return a properly formatted signed URL |
| 45 | + * @throws InvalidKeyException when there is an error generating the signature for the input key |
| 46 | + * @throws NoSuchAlgorithmException when HmacSHA1 algorithm is not available in the environment |
| 47 | + */ |
| 48 | + public static String signUrl(String url, |
| 49 | + byte[] key, |
| 50 | + String keyName, |
| 51 | + Date expirationTime) |
| 52 | + throws InvalidKeyException, NoSuchAlgorithmException { |
| 53 | + |
| 54 | + final long unixTime = expirationTime.getTime() / 1000; |
| 55 | + |
| 56 | + String urlToSign = url |
| 57 | + + (url.contains("?") ? "&" : "?") |
| 58 | + + "Expires=" + unixTime |
| 59 | + + "&KeyName=" + keyName; |
| 60 | + |
| 61 | + String encoded = SignedUrls.getSignature(key, urlToSign); |
| 62 | + return urlToSign + "&Signature=" + encoded; |
| 63 | + } |
| 64 | + |
| 65 | + public static String getSignature(byte[] privateKey, String input) |
| 66 | + throws InvalidKeyException, NoSuchAlgorithmException { |
| 67 | + |
| 68 | + final String algorithm = "HmacSHA1"; |
| 69 | + final int offset = 0; |
| 70 | + Key key = new SecretKeySpec(privateKey, offset, privateKey.length, algorithm); |
| 71 | + Mac mac = Mac.getInstance(algorithm); |
| 72 | + mac.init(key); |
| 73 | + return Base64.getUrlEncoder().encodeToString(mac.doFinal(input.getBytes())); |
| 74 | + } |
| 75 | + // [END signUrl] |
| 76 | + |
| 77 | + public static void main(String[] args) throws Exception { |
| 78 | + Calendar cal = Calendar.getInstance(); |
| 79 | + cal.setTime(new Date()); |
| 80 | + cal.add(Calendar.DATE, 1); |
| 81 | + Date tomorrow = cal.getTime(); |
| 82 | + |
| 83 | + //read the key as a base 64 url-safe encoded string, then convert to byte array |
| 84 | + final String keyPath = "/path/to/key"; |
| 85 | + String base64String = new String(Files.readAllBytes(Paths.get(keyPath))); |
| 86 | + byte[] keyBytes = Base64.getUrlDecoder().decode(base64String); |
| 87 | + |
| 88 | + String result = signUrl("http://example.com/", keyBytes, "YOUR-KEY-NAME", tomorrow); |
| 89 | + System.out.println(result); |
| 90 | + } |
| 91 | +} |
0 commit comments