24
24
import java .security .Key ;
25
25
import java .security .PrivateKey ;
26
26
import java .security .Provider ;
27
+ import java .time .Clock ;
28
+ import java .time .Instant ;
29
+ import java .time .ZoneId ;
27
30
import java .util .Arrays ;
28
31
import java .util .HashMap ;
29
32
import java .util .Map ;
@@ -95,7 +98,7 @@ public void testSign() throws Exception {
95
98
final Signature signature = new Signature ("hmac-key-1" , SigningAlgorithm .HS2019 .getAlgorithmName (), "hmac-sha256" , null , null , Arrays .asList ("content-length" , "host" , "date" , "(request-target)" ));
96
99
97
100
final Key key = new SecretKeySpec ("don't tell" .getBytes (), "HmacSHA256" );
98
- final Signer signer = new Signer (key , signature );
101
+ final Signer signer = new Signer (key , signature , Clock . fixed ( Instant . ofEpochMilli ( 123456789l ), ZoneId . systemDefault ()) );
99
102
100
103
{
101
104
final String method = "GET" ;
@@ -109,8 +112,8 @@ public void testSign() throws Exception {
109
112
headers .put ("Content-Length" , "18" );
110
113
final Signature signed = signer .sign (method , uri , headers );
111
114
assertEquals ("yT/NrPI9mKB5R7FTLRyFWvB+QLQOEAvbGmauC0tI+Jg=" , signed .getSignature ());
112
- assertToString ("Signature keyId=\" hmac-key-1\" ,created=9999 ,algorithm=\" hs2019\" ," +
113
- "headers=\" content-length host date (request-target)\" ,signature=\" yT/NrPI9mKB5R7FTLRyFWvB+QLQOEAvbGmauC0tI+Jg=\" " , signed );
115
+ assertEquals ("Signature keyId=\" hmac-key-1\" ,created=123456 ,algorithm=\" hs2019\" ," +
116
+ "headers=\" content-length host date (request-target)\" ,signature=\" yT/NrPI9mKB5R7FTLRyFWvB+QLQOEAvbGmauC0tI+Jg=\" " , signed . toString () );
114
117
}
115
118
116
119
{ // method changed. should get a different signature
@@ -125,8 +128,8 @@ public void testSign() throws Exception {
125
128
headers .put ("Content-Length" , "18" );
126
129
final Signature signed = signer .sign (method , uri , headers );
127
130
assertEquals ("DPIsA/PWeYjySmfjw2P2SLJXZj1szDOei/Hh8nTcaPo=" , signed .getSignature ());
128
- assertToString ("Signature keyId=\" hmac-key-1\" ,created=9999 ,algorithm=\" hs2019\" ," +
129
- "headers=\" content-length host date (request-target)\" ,signature=\" DPIsA/PWeYjySmfjw2P2SLJXZj1szDOei/Hh8nTcaPo=\" " , signed );
131
+ assertEquals ("Signature keyId=\" hmac-key-1\" ,created=123456 ,algorithm=\" hs2019\" ," +
132
+ "headers=\" content-length host date (request-target)\" ,signature=\" DPIsA/PWeYjySmfjw2P2SLJXZj1szDOei/Hh8nTcaPo=\" " , signed . toString () );
130
133
}
131
134
132
135
{ // only Digest changed. not part of the signature, should have no effect
@@ -141,8 +144,8 @@ public void testSign() throws Exception {
141
144
headers .put ("Content-Length" , "18" );
142
145
final Signature signed = signer .sign (method , uri , headers );
143
146
assertEquals ("DPIsA/PWeYjySmfjw2P2SLJXZj1szDOei/Hh8nTcaPo=" , signed .getSignature ());
144
- assertToString ("Signature keyId=\" hmac-key-1\" ,created=1628283435 ,algorithm=\" hs2019\" ," +
145
- "headers=\" content-length host date (request-target)\" ,signature=\" DPIsA/PWeYjySmfjw2P2SLJXZj1szDOei/Hh8nTcaPo=\" " , signed );
147
+ assertEquals ("Signature keyId=\" hmac-key-1\" ,created=123456 ,algorithm=\" hs2019\" ," +
148
+ "headers=\" content-length host date (request-target)\" ,signature=\" DPIsA/PWeYjySmfjw2P2SLJXZj1szDOei/Hh8nTcaPo=\" " , signed . toString () );
146
149
}
147
150
148
151
{ // uri changed. should get a different signature
@@ -157,19 +160,11 @@ public void testSign() throws Exception {
157
160
headers .put ("Content-Length" , "18" );
158
161
final Signature signed = signer .sign (method , uri , headers );
159
162
assertEquals ("IWTDxmOoEJI67YxY3eDIRzxrsAtlYYCuGZxKlkUSYdA=" , signed .getSignature ());
160
- assertToString ("Signature keyId=\" hmac-key-1\" ,created=9999 ,algorithm=\" hs2019\" ," +
161
- "headers=\" content-length host date (request-target)\" ,signature=\" IWTDxmOoEJI67YxY3eDIRzxrsAtlYYCuGZxKlkUSYdA=\" " , signed );
163
+ assertEquals ("Signature keyId=\" hmac-key-1\" ,created=123456 ,algorithm=\" hs2019\" ," +
164
+ "headers=\" content-length host date (request-target)\" ,signature=\" IWTDxmOoEJI67YxY3eDIRzxrsAtlYYCuGZxKlkUSYdA=\" " , signed . toString () );
162
165
}
163
166
}
164
167
165
- private void assertToString (final String expected , final Signature signed ) {
166
- assertEquals (normalize (expected ), normalize (signed .toString ()));
167
- }
168
-
169
- private static String normalize (final String s ) {
170
- return s .replaceAll ("(created)=[0-9]+" , "$1=9999" );
171
- }
172
-
173
168
@ Test
174
169
public void defaultHeaderList () throws Exception {
175
170
final Signature signature = new Signature ("hmac-key-1" , SigningAlgorithm .HS2019 .getAlgorithmName (), "hmac-sha256" ,
0 commit comments