Skip to content

Instantly share code, notes, and snippets.

@manan-jadhav
Last active September 20, 2022 18:11
Show Gist options
  • Select an option

  • Save manan-jadhav/21aadc2e5ce8ecfefad0f283454963ba to your computer and use it in GitHub Desktop.

Select an option

Save manan-jadhav/21aadc2e5ce8ecfefad0f283454963ba to your computer and use it in GitHub Desktop.
Generate AWS SES SMTP Password from AWS Access Key
/*
When using AWS SES and attempting to send emails over SMTP using
an custom IAM user, the user needs to have the SendRawEmail permission.
The SMTP username is the AWS Access Key ID while the SMTP password must
be calculated using a *special* (ahem, ahem) algorithm mentioned in AWS
docs here: https://docs.aws.amazon.com/ses/latest/dg/smtp-credentials.html
This function is a Node.js compliant port of the pseudocode mentioned on
that page.
*/
const crypto = require('crypto');
function generateAwsSesSmtpPassword(accessKey, region) {
const date = "11111111";
const service = "ses";
const message = "SendRawEmail";
const terminal = "aws4_request";
const version = 0x04;
const sign = (key, msg) => {
return crypto.createHmac('sha256', key).update(msg).digest();
}
let signature = sign("AWS4" + accessKey, date);
signature = sign(signature, region);
signature = sign(signature, service);
signature = sign(signature, terminal);
signature = sign(signature, message);
return Buffer.concat([Buffer.from([version]), signature]).toString('base64');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment