1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
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  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */

15
16 package software.amazon.awssdk.auth.signer.internal;
17
18 import java.time.Instant;
19 import software.amazon.awssdk.annotations.Immutable;
20 import software.amazon.awssdk.annotations.SdkInternalApi;
21 import software.amazon.awssdk.utils.DateUtils;
22
23 /**
24  * Holds the signing key and the number of days since epoch for the date for
25  * which the signing key was generated.
26  */

27 @Immutable
28 @SdkInternalApi
29 public final class SignerKey {
30
31     private final long daysSinceEpoch;
32
33     private final byte[] signingKey;
34
35     public SignerKey(Instant date, byte[] signingKey) {
36         if (date == null) {
37             throw new IllegalArgumentException(
38                     "Not able to cache signing key. Signing date to be is null");
39         }
40         if (signingKey == null) {
41             throw new IllegalArgumentException(
42                     "Not able to cache signing key. Signing Key to be cached are null");
43         }
44         this.daysSinceEpoch = DateUtils.numberOfDaysSinceEpoch(date.toEpochMilli());
45         this.signingKey = signingKey.clone();
46     }
47
48     public boolean isValidForDate(Instant other) {
49         return daysSinceEpoch == DateUtils.numberOfDaysSinceEpoch(other.toEpochMilli());
50     }
51
52     /**
53      * Returns a copy of the signing key.
54      */

55     public byte[] getSigningKey() {
56         return signingKey.clone();
57     }
58 }
59