<commit_before>var crypto = require( 'crypto' )
var DKIM = require( './dkim' )

/**
 * Verify a message signature
 * @memberOf DKIM
 * @param {Buffer} body
 * @param {Array} headers
 * @param {Function} callback
 */
function verifySignature( body, headers, callback ) {

  var signature = null
  // NOTE: result.status counts only if verified is not true
  var result = {
    verified: false,
    status: DKIM.NONE,
    error: null,
    signature: null,
    key: null
  }

  try {
    if( !/^(DKIM-Signature|X-Google-DKIM-Signature)/i.test( headers[0] ) ) {
      throw new Error( 'Missing DKIM-Signature' )
    }
    signature = result.signature = DKIM.Signature.parse( headers[0].slice( headers[0].indexOf(':') ) )
  } catch( e ) {
    result.error = new Error( 'Signature syntax error' )
    result.error.code = result.status = DKIM.PERMFAIL
    return callback( result.error, result )
  }

  // Truncate body to defined length
  body = signature.length != null ?
    body.slice( 0, signature.length ) :
    body

  DKIM.getKey( signature.domain, signature.selector, ( error, key ) => {

    if( error != null ) {
      result.error = error
      result.status = error.code
      return callback( error, result )
    }

    result.key = key<commit_msg>fix(lib): Fix header slice offset in verifySignature()<commit_after>var crypto = require( 'crypto' )
var DKIM = require( './dkim' )

/**
 * Verify a message signature
 * @memberOf DKIM
 * @param {Buffer} body
 * @param {Array} headers
 * @param {Function} callback
 */
function verifySignature( body, headers, callback ) {

  var signature = null
  // NOTE: result.status counts only if verified is not true
  var result = {
    verified: false,
    status: DKIM.NONE,
    error: null,
    signature: null,
    key: null
  }

  try {
    if( !/^(DKIM-Signature|X-Google-DKIM-Signature)/i.test( headers[0] ) ) {
      throw new Error( 'Missing DKIM-Signature' )
    }
    signature = result.signature = DKIM.Signature.parse( headers[0].slice( headers[0].indexOf(':') + 1 ) )
  } catch( err ) {
    result.error = err
    result.error.code = result.status = DKIM.PERMFAIL
    return callback( result.error, result )
  }

  // Truncate body to defined length
  body = signature.length != null ?
    body.slice( 0, signature.length ) :
    body

  DKIM.getKey( signature.domain, signature.selector, ( error, key ) => {

    if( error != null ) {
      result.error = error
      result.status = error.code
      return callback( error, result )
    }

    result.key = key
{'fraction_non_alphanumeric': 0.13238208481965913, 'fraction_numerical': 0.0035671819262782403, 'mean_word_length': 2.859327217125382, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5967412', 'n_tokens_mistral': 813, 'n_tokens_neox': 795, 'n_words': 224}