Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkokiefer committed Nov 7, 2013
1 parent f51873b commit 9278f88
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
14 changes: 4 additions & 10 deletions lib/aws.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@

var http = require("http");
var https = require("https");
var qs = require("querystring")
var crypto = require("crypto")
var events = require("events")
var xml2js = require("xml2js")
var utils = require('./utils')

// include specific API clients
var ec2 = require("./ec2");
Expand Down Expand Up @@ -36,15 +37,8 @@ exports.createCFNClient = cfn.init(genericAWSClient);
exports.createEMRClient = emr.init(genericAWSClient);
exports.createMetaDataClient = metadata.init();

// Returns the hmac digest using the SHA256 algorithm.
function hmacSha256(key, toSign) {
var hash = crypto.createHmac("sha256", key);
return hash.update(toSign).digest("base64");
}

// a generic AWS API Client which handles the general parts
function genericAWSClient(obj) {
var creds = crypto.createCredentials({});
if (null == obj.secure)
obj.secure = true;

Expand Down Expand Up @@ -85,7 +79,7 @@ function genericAWSClient(obj) {
"AWS3-HTTPS " +
"AWSAccessKeyId=" + obj.accessKeyId + ", " +
"Algorithm=HmacSHA256, " +
"Signature=" + hmacSha256(obj.secretAccessKey, now.toUTCString());
"Signature=" + utils.hmacSha256(obj.secretAccessKey, now.toUTCString());
}

var options = {
Expand Down Expand Up @@ -151,7 +145,7 @@ function genericAWSClient(obj) {
stringToSign = stringToSign.replace(/\(/g,"%28");
stringToSign = stringToSign.replace(/\)/g,"%29");

return hmacSha256(obj.secretAccessKey, stringToSign);
return utils.hmacSha256(obj.secretAccessKey, stringToSign);
}
return obj;
}
7 changes: 7 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

var crypto = require("crypto")

exports.hmacSha256 = function (key, toSign) {
var hash = crypto.createHmac("sha256", key);
return hash.update(toSign).digest("base64");
}

0 comments on commit 9278f88

Please sign in to comment.