Created
October 27, 2014 20:28
-
-
Save sparkyfen/1cb8093e0573b835bdca to your computer and use it in GitHub Desktop.
Keybase.io Login w/NodeJS using Request, Crypto, and Scrypt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get salt: https://keybase.io/docs/api/1.0/call/getsalt | |
// Login: https://keybase.io/docs/api/1.0/call/login | |
var request = require('request'); | |
var scrypt = require('scrypt'); | |
var crypto = require('crypto'); | |
var username = "myUsername"; | |
var password = "myPassword"; | |
request('https://keybase.io/_/api/1.0/getsalt.json?email_or_username=' + username, function (error, body, results) { | |
if(error) { | |
console.log(error); | |
return; | |
} | |
results = JSON.parse(results); | |
if(results.status.code === 0) { | |
var loginSession = results.login_session; | |
var csrfToken = results.csrf_token; | |
var salt = results.salt; | |
scrypt.kdf.config.saltEncoding = "ascii"; | |
scrypt.kdf.config.keyEncoding = "ascii"; | |
var bytes = []; | |
for(var i=0; i< salt.length-1; i+=2){ | |
bytes.push(parseInt(salt.substr(i, 2), 16)); | |
} | |
var binarySalt = String.fromCharCode.apply(String, bytes); | |
scrypt.kdf(password, {N: Math.pow(2,15), r:8, p:1}, 224, binarySalt, function(err, result){ | |
var hash = crypto.createHmac('sha512', result.hash.slice(192, 224)); | |
hash.update(new Buffer(loginSession, 'base64')); | |
var hashedData = hash.digest('hex'); | |
request.post('https://keybase.io/_/api/1.0/login.json', {form: { | |
csrf_token: csrfToken, | |
login_session: loginSession, | |
email_or_username: username, | |
hmac_pwh: hashedData | |
}}, function (error, body, response) { | |
if(error) { | |
console.log(error); | |
return; | |
} | |
console.log(response); | |
}); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment