|
|
|
# Signing CoAP requests with jRECMA
|
|
|
|
|
|
|
|
```java
|
|
|
|
byte[] key = Base64.getMimeDecoder().decode(base64Key);
|
|
|
|
|
|
|
|
HashMap<Integer, byte[]> hmacKeyStore = new HashMap<>();
|
|
|
|
|
|
|
|
hmacKeyStore.put(1, key);
|
|
|
|
|
|
|
|
HmacSha256Authenticator hmacSha256Authenticator = new HmacSha256Authenticator(hmacKeyStore);
|
|
|
|
|
|
|
|
Sha256Hasher sha256Hasher = new Sha256Hasher();
|
|
|
|
|
|
|
|
RequestSigner requestSigner = new RequestSigner((short)1,hmacSha256Authenticator, sha256Hasher);
|
|
|
|
CoapClient client = new CoapClient();
|
|
|
|
|
|
|
|
CoapRequest req = new CoapRequest(0, 1);
|
|
|
|
|
|
|
|
int messageID = new SecureRandom().nextInt(1000);
|
|
|
|
|
|
|
|
req.setMessageID(messageID);
|
|
|
|
|
|
|
|
requestSigner.sign(req);
|
|
|
|
|
|
|
|
client.sendCoapRequest(req, new InetSocketAddress("coap.me", 5683),new ClientCallback() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void processCoapResponse(CoapResponse coapResponse) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
System.out.println(coapResponse);
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
System.out.println(req);
|
|
|
|
``` |
|
|
|
\ No newline at end of file |