Skip to content

opendkim.sign()

Christopher Mooney edited this page Feb 12, 2018 · 3 revisions

DESCRIPTION

Create a new context for signing a message. opendkim.sign() is called when preparing to process a new message that will
be signed later by a private key.

For more information:
http://www.opendkim.org/libopendkim/dkim_sign.html

ARGUMENTS

Type: Object

  • id: (default: undefined) An opaque, printable string for identifying this message, suitable for use in logging or debug output.
  • secretkey: The private key to be used when signing this message. This must be a string containing either a PEM-formatted private key, or a DER-formatted private key after being encoded with base64.
  • selector: The name of the selector to be reported in the signature on this message.
  • domain: The domain doing the signing; this will be the domain whose DNS will be queried by the verifier for key data.
  • hdrcanon: (values: 'simple | relaxed') (default: 'simple') The canonicalization algorithm to use when preparing the headers of this message for signing.
  • bodycanon: (values: 'simple | relaxed') (default: 'simple') The canonicalization algorithm to use when preparing the body of this message for signing.
  • signalg: (values: 'sha1 | sha256') (default: 'sha256') The signing algorithm to use when generating the signature to be attached to this message.
  • length: (default: -1) The number of bytes of the body to sign. A value of -1 will cause the entire message to be signed.

RETURN VALUES

  • On failure, an exception is thrown that indicates the cause of the problem.

NOTES

  • DKIM_STAT_INVALID is thrown if, for example, a signing handle using 'sha256' is requested when the library was not compiled against a version of OpenSSL that had support for that hash algorithm.
  • The context for signing is now stored -- under the hood -- with the OpenDKIM instance.

EXAMPLE (async/await)

const OpenDKIM = require('node-opendkim');                                                               
                                                                                                         
async function sign(message) {                                                                           
  try {                                                                                                  
    var opendkim = new OpenDKIM();                                                                       
    await opendkim.sign({                                                                                
      id: undefined, // optional (default: undefined)                                                    
      secretkey: 'testkey',                                                                              
      selector: 'a1b2c3',                                                                                
      domain: 'example.com',                                                                             
      hdrcanon: 'relaxed',                                                                               
      bodycanon: 'relaxed',                                                                              
      signalg: 'sha256',                                                                                 
      signalg: 'sha256',                                                                                 
      length: -1     // optional (default: -1)                                                           
    });                                                                                                  
  } catch (err) {                                                                                        
    console.log(err);                                                                                    
  }                                                                                                      
}                                                                                                        

EXAMPLE (sync)

const OpenDKIM = require('node-opendkim');                                                               
                                                                                                         
function sign_sync(message) {                                                                            
  try {                                                                                                  
    var opendkim = new OpenDKIM();                                                                       
    opendkim.sign_sync({                                                                                 
      id: undefined, // optional (default: undefined)                                                    
      secretkey: 'testkey',                                                                              
      selector: 'a1b2c3',                                                                                
      domain: 'example.com',                                                                             
      hdrcanon: 'relaxed',                                                                               
      bodycanon: 'relaxed',                                                                              
      signalg: 'sha256',                                                                                 
      signalg: 'sha256',                                                                                 
      length: -1     // optional (default: -1)                                                           
    });                                                                                                  
  } catch (err) {                                                                                        
    console.log(err);                                                                                    
  }                                                                                                      
}                                                                                                        

EXAMPLE (errback)

const OpenDKIM = require('node-opendkim');                                                               
                                                                                                         
function sign(message, callback) {                                                                       
  var opendkim = new OpenDKIM();                                                                         
                                                                                                         
  var options = {                                                                                        
    id: undefined, // optional (default: undefined)                                                      
    secretkey: 'testkey',                                                                                
    selector: 'a1b2c3',                                                                                  
    domain: 'example.com',                                                                               
    hdrcanon: 'relaxed',                                                                                 
    bodycanon: 'relaxed',                                                                                
    signalg: 'sha256',                                                                                   
    signalg: 'sha256',                                                                                   
    length: -1     // optional (default: -1)                                                             
  });                                                                                                    
                                                                                                         
  opendkim.sign(options, function (err, result) {                                                        
    if (err) {                                                                                           
      return callback(err, result);                                                                      
    }                                                                                                    
                                                                                                         
    callback(err, result);                                                                               
  });                                                                                                    
}                                                                                                        
                                                                                                         
sign(message, function (err, result) {                                                                   
  if (err) {                                                                                             
    return console.log(err);                                                                                    
  }                                                                                                      
                                                                                                         
  // success                                                                                             
});