text: /**
   * Copyright 2012-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
   *
   * Licensed under the Apache License, Version 2.0 (the "License"). You
   * may not use this file except in compliance with the License. A copy of
   * the License is located at
   *
   *     http://aws.amazon.com/apache2.0/
   *
   * or in the "license" file accompanying this file. This file is
   * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
   * ANY KIND, either express or implied. See the License for the specific
   * language governing permissions and limitations under the License.
   */
  
  var AWS = require('../core');
  var inherit = AWS.util.inherit;
  
  /**
   * @api private
   */
  AWS.Signers.V3 = inherit(AWS.Signers.RequestSigner, {
    addAuthorization: function addAuthorization(credentials, date) {
  
      var datetime = AWS.util.date.rfc822(date);
  
      this.request.headers['Date'] = datetime;
      this.request.headers['X-Amz-Date'] = datetime;
  
      if (credentials.sessionToken) {
        this.request.headers['X-Amz-Security-Token'] = credentials.sessionToken;
      }
  
      this.request.headers['X-Amzn-Authorization'] =
        this.authorization(credentials, datetime);
  
    },
  
    authorization: function authorization(credentials) {
      return 'AWS3 ' +
        'AWSAccessKeyId=' + credentials.accessKeyId + ',' +
        'Algorithm=HmacSHA256,' +
        'SignedHeaders=' + this.signedHeaders() + ',' +
        'Signature=' + this.signature(credentials);
    },
  
    signedHeaders: function signedHeaders() {
      var headers = [];
      AWS.util.arrayEach(this.headersToSign(), function iterator(h) {
        headers.push(h.toLowerCase());
      });
      return headers.sort().join(';');
    },
  
    canonicalHeaders: function canonicalHeaders() {
      var headers = this.request.headers;
      var parts = [];
      AWS.util.arrayEach(this.headersToSign(), function iterator(h) {
        parts.push(h.toLowerCase().trim() + ':' + String(headers[h]).trim());
      });
      return parts.sort().join('\n') + '\n';
    },
  
    headersToSign: function headersToSign() {
      var headers = [];
      AWS.util.each(this.request.headers, function iterator(k) {
        if (k === 'Host' || k === 'Content-Encoding' || k.match(/^X-Amz/i)) {
          headers.push(k);
        }
      });
      return headers;
    },
  
    signature: function signature(credentials) {
      return AWS.util.crypto.hmac(credentials.secretAccessKey, this.stringToSign(), 'base64');
    },
  
    stringToSign: function stringToSign() {
      var parts = [];
      parts.push(this.request.method);
      parts.push('/');
      parts.push('');
      parts.push(this.canonicalHeaders());
      parts.push(this.request.body);
      return AWS.util.crypto.sha256(parts.join('\n'));
    }
  
  });
  
  module.exports = AWS.Signers.V3;
  