Sample code for 30+ languages & platforms
Java

Set the JWS Protected Header

See more JSON Web Signatures (JWS) Examples

Demonstrates Jws.SetProtectedHeader, which sets the integrity-protected header for a signer from a JsonObject. The header specifies the signature algorithm (alg).

Background. The protected header is covered by the signature, so it cannot be altered without invalidating the JWS. It is configured before creating the JWS.

Chilkat Java Downloads

Chilkat2 Java
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();

        // Build the JWS protected header.  The "alg" member names the signature algorithm.
        JsonObject header = new JsonObject();
        header.updateString("alg", "HS256");

        // Set the protected header for signer index 0.  The protected header is integrity-protected: it is
        // covered by the signature.
        try {
            jws.setProtectedHeader(0, header);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("Protected header set.");
    }
}