Javascript: Convert Yubico “modhex” strings to hex
small snippet
Are you using a Yubikey and want to create your custom Keyserver written in Node.js ? In this case this piece of code might be useful :)
/**
* Convert the Yubico MODHEX encoded Strings to hex
* @param modhex String
* @returns hex String
*/
var modhex2hex = function(modhex){
// strip whitespaces and string cleanup - all non matching characters are 0x00 (c in modhex)
modhex = modhex.replace(/\s*/g, '').replace(/[^cbdefghijklnrtuv]/g, 'c');
// even length ?
if (modhex.length%2 !== 0){
return null;
}
// modhex mapping base; c.....v => 0x0 ... 0xF
var modhexBase = 'cbdefghijklnrtuv'.split('');
// tmp
var output = '';
// convert
for (var i=0;i<modhex.length;i++){
// convert index to hex
output += modhexBase.indexOf(modhex.charAt(i)).toString(16);
}
return output;
};
console.log(modhex2hex('te vt hh fg ue dk gv rt lv hb lu gf nk ge ng cv'));