Java
Chilkat2 Java
Sign a JWS with an HMAC Key from BinData
See more JSON Web Signatures (JWS) Examples
Demonstrates Jws.SetMacKeyBd, which sets the symmetric MAC key for a signature from the raw bytes in a BinData.
Background. This is the in-memory equivalent of SetMacKey, for when the key material is already available as bytes.
Chilkat Java Downloads
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Jws;
public class ChilkatExample {
public static void main(String[] args) {
Jws jws = new Jws();
// Protected header specifying HMAC-SHA256.
JsonObject header = new JsonObject();
header.updateString("alg", "HS256");
try {
jws.setProtectedHeader(0, header);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
boolean bIncludeBom = false;
try {
jws.setPayload("This is the content to sign.", "utf-8", bIncludeBom);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Set the symmetric MAC key for signature 0 from the raw bytes in a BinData. In production, obtain
// the key material from a secure source.
BinData bdKey = new BinData();
bdKey.appendEncoded("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU=", "base64");
String jwsCompact;
try {
jws.setMacKeyBd(0, bdKey);
jwsCompact = jws.createJws();
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println(jwsCompact);
}
}