JWSSigner class

The JWSSigner class is used for the digital sign or MAC operation.

The JWSSigner class provides the following class methods.

JWSSigner.sign()

Signs the JWS content.

Syntax
JWSSigner.sign([format],function(error,JWS))
Parameters
format
The serialization format, which is a string that is 'compact', 'json', or 'json_flat'. The default value is 'compact'.
  • 'compact' indicates the compact serialization.
  • 'json' indicates the general JSON serialization.
  • 'json_flat' indicates the flattened JOSN serialization. Flattened JSON serialization is available when only one signature exists.
.
error
The error information if an error occurs.
JWS
The output of the signature.
Examples
  • Compact serialization.
    var jose = require('jose');
    // compact serialization
    var hdr = jose.createJWSHeader(key, 'HS256');
    var mySign = jose.createJWSSigner(hdr);
    mySign.update("A string to sign");
    mySign.sign('compact',function(error, JWSstring) {
      if (error) {
        session.output.write("jws sign fail: "+error);
      } else {
        session.output.write(jwsString);
      }
    });
  • General JSON serialization.
    var jose = require('jose');
    // JSON serialization
    var hdr1 = jose.createJWSHeader(key, 'HS256');
    hdr1.setUnprotected({'kid': "kid1"});
    var hdr2 = jose.createJWSHeader(key);
    hdr2.setUnprotected('alg', 'RS256');
    hdr2.setUnprotected('kid', "kid2");
    var mySign = jose.createJWSSigner(hdr1,hdr2);
    mySign.update("A string to sign");
    mySign.sign('json',function(error, JWSstring) {
      if (error) {
        session.output.write("jws sign fail: "+error);
      } else {
        session.output.write(JWSstring);
      }
    });
  • Flattened JSON serialization.
    var jose = require('jose');
    // compact serialization
    var hdr = jose.createJWSHeader(key, 'HS256');
    var mySign = jose.createJWSSigner(hdr);
    mySign.update("A string to sign");
    mySign.sign('json_flat',function(error, JWSstring) {
      if (error) {
        session.output.write("jws sign fail: "+error);
      } else {
        session.output.write(jwsString);
      }
    });

JWSSigner.update()

Populates the payload into a JWS signature object.

Syntax
JWSSigner.update(payload)
Parameters
payload
A sequence of octets to be secured. The payload must be String, Buffer, or Buffers. Call JSON.stringify() before you pass the payload into this function if needed.