Skip to content

Instantly share code, notes, and snippets.

@jeanouii
Created February 2, 2015 23:29
Show Gist options
  • Select an option

  • Save jeanouii/9b47c529aead9483c4af to your computer and use it in GitHub Desktop.

Select an option

Save jeanouii/9b47c529aead9483c4af to your computer and use it in GitHub Desktop.
public class DummyTest {
@Test
public void calcDigest() throws Exception {
final Signature signature = new Signature("kids", "hmac-sha256", null, "(request-target)", "date");
final SecretKey key = new SecretKeySpec("PennyLunaLuka".getBytes(), "HmacSHA256");
final Signer signer = new Signer(key, signature);
final String method = "GET";
final String uri = "/signature-prototype-1.0-SNAPSHOT/api/colors/preferred";
final Map<String, String> headers = new HashMap<>();
headers.put("Date", "Tue, 07 Jun 2014 20:51:35 GMT");
final Signature result = signer.sign(method, uri, headers);
System.out.println(result);
final String signingString = Signatures.createSigningString(Arrays.asList("(request-target) date".split(" ")), method, uri, headers);
System.out.println(signingString);
System.out.println(new String(Base64.encodeBase64(signingString.getBytes()), "UTF-8"));
final String expected = "KHJlcXVlc3QtdGFyZ2V0KTogZ2V0IC9zaWduYXR1cmUtcHJvdG90eXBlLTEuMC1TTkFQU0hPVC9hcGkvY29sb3JzL3ByZWZlcnJlZApkYXRlOiBUdWUsIDA3IEp1biAyMDE0IDIwOjUxOjM1IEdNVA==";
System.out.println(expected);
System.out.println(new String(Base64.decodeBase64(expected.getBytes()), "UTF-8"));
System.out.println(expected.equals(new String(Base64.encodeBase64(signingString.getBytes()), "UTF-8")));
System.out.println(new String(Base64.decodeBase64("KHJlcXVlc3QtdGFyZ2V0KTogZ2V0IC9zaWduYXR1cmUtcHJvdG90eXBlLTEuMC1TTkFQU0hPVC9hcGkvY29sb3JzL3ByZWZlcnJlZFxuZGF0ZTogVHVlLCAwNyBKdW4gMjAxNCAyMDo1MTozNSBHTVQ=".getBytes()), "UTF-8"));
}
}
@jeanouii

jeanouii commented Feb 2, 2015

Copy link
Copy Markdown
Author

Signature keyId="kids",algorithm="hmac-sha256",headers="(request-target) date",signature="g+GNqWOXhigFV9ujbZN50J54HDeNZa1L4Bom+HIozqE="
(request-target): get /signature-prototype-1.0-SNAPSHOT/api/colors/preferred
date: Tue, 07 Jun 2014 20:51:35 GMT
KHJlcXVlc3QtdGFyZ2V0KTogZ2V0IC9zaWduYXR1cmUtcHJvdG90eXBlLTEuMC1TTkFQU0hPVC9hcGkvY29sb3JzL3ByZWZlcnJlZApkYXRlOiBUdWUsIDA3IEp1biAyMDE0IDIwOjUxOjM1IEdNVA==
KHJlcXVlc3QtdGFyZ2V0KTogZ2V0IC9zaWduYXR1cmUtcHJvdG90eXBlLTEuMC1TTkFQU0hPVC9hcGkvY29sb3JzL3ByZWZlcnJlZApkYXRlOiBUdWUsIDA3IEp1biAyMDE0IDIwOjUxOjM1IEdNVA==
(request-target): get /signature-prototype-1.0-SNAPSHOT/api/colors/preferred
date: Tue, 07 Jun 2014 20:51:35 GMT
true
(request-target): get /signature-prototype-1.0-SNAPSHOT/api/colors/preferred\ndate: Tue, 07 Jun 2014 20:51:35 GMT

@jeanouii

jeanouii commented Feb 2, 2015

Copy link
Copy Markdown
Author

$ curl -D h.dump -H 'Authorization: Signature keyId="kids",algorithm="hmac-sha256",headers="(request-target) date",signature="g+GNqWOXhigFV9ujbZN50J54HDeNZa1L4Bom+HIozqE="' -H 'Date: Tue, 07 Jun 2014 20:51:35 GMT' http://ec2-54-165-89-69.compute-1.amazonaws.com:8080/signature-prototype-1.0-SNAPSHOT/api/colors/preferred

@jeanouii

jeanouii commented Feb 2, 2015

Copy link
Copy Markdown
Author

openssl dgst -sha256 -hmac "PennyLunaLuka"

@jeanouii

jeanouii commented Feb 2, 2015

Copy link
Copy Markdown
Author

$ echo -e "(request-target): get /signature-prototype-1.0-SNAPSHOT/api/colors/preferred
date: Tue, 07 Jun 2014 20:51:35 GMT" | openssl dgst -sha256 -hmac "PennyLunaLuka" | base64
NjJhYjI3ZDliZjYxNTkxOTY0NDU3YWZiNzcwOTY4OTBjMDM3NGI3YTY3NjQ5NjEwZTkxMDEyNzZkYmM0Y2I5OAo=

@jeanouii

jeanouii commented Feb 3, 2015

Copy link
Copy Markdown
Author

@command
public String createKeyStore(final @option("keystore") @required File keyStore,
final @option("password") @required String keystorePassword,
final @option("keyalias") @required String keyAlias,
final @option("secret") @required String secret) throws Exception {

    if (! keyStore.exists()) {
        StoreManager.get(keyStore.getAbsolutePath(), keystorePassword).init();
    }

    final byte[] secretBytes = secret.getBytes();
    SecretKey secretKey = new SecretKeySpec(secretBytes, 0, secretBytes.length, "hmac-sha256");
    StoreManager.get(keyStore.getAbsolutePath(), keystorePassword).addKey(keyAlias, secretKey);

    return "Stored\n";
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment