Skip to content

Instantly share code, notes, and snippets.

@elementechemlyn
Created July 8, 2020 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elementechemlyn/88e710bb79977e16b9f81ffe04366718 to your computer and use it in GitHub Desktop.
Save elementechemlyn/88e710bb79977e16b9f81ffe04366718 to your computer and use it in GitHub Desktop.
function meshAuthorisationToken (mailboxId, mailboxPassword, secretKey) {
// GS 2016-12-13
// construct MESH authorisation token
var d = new Date();
var ts = '' + d.getFullYear()
+ (d.getMonth() < 9 ? '0' + (d.getMonth()+1) : (d.getMonth()+1))
+ (d.getDate() < 9 ? '0' + (d.getDate()) : (d.getDate()))
+ (d.getHours() < 10 ? '0' + d.getHours() : d.getHours())
+ (d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes());
var nonce = generateUuid();
var nonceCount = 1;
var msg = java.lang.String(mailboxId + ':' + nonce + ':' + nonceCount + ':' + mailboxPassword + ':' + ts);
var secretStr = java.lang.String(secretKey);
var mac = javax.crypto.Mac.getInstance("HmacSHA256");
var secret = new javax.crypto.spec.SecretKeySpec(secretStr.getBytes("UTF-8"), "HmacSHA256");
mac.init(secret);
var hash = javax.xml.bind.DatatypeConverter.printHexBinary(mac.doFinal(msg.getBytes("UTF-8"))).toLowerCase();
var auth = 'NHSMESH ' + mailboxId + ':' + nonce + ':' + nonceCount + ':' + ts + ':' + hash;
return auth;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment