Skip to content

Instantly share code, notes, and snippets.

@christocracy
Last active November 14, 2019 16:07
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 christocracy/f814dd35cfd9eced5d4de3025c38333c to your computer and use it in GitHub Desktop.
Save christocracy/f814dd35cfd9eced5d4de3025c38333c to your computer and use it in GitHub Desktop.
Background Geolocation Description at Server with NodeJS
/**
* Node JS decryption example of data encrypted by BackgroundGeolocation SDK.
* See RNCrypto Data Format Spec:
* https://github.com/RNCryptor/RNCryptor-Spec/blob/master/RNCryptor-Spec-v3.md
*/
var crypto = require('crypto');
// Decryption password. Same as used by BackgroundGeolocation SDK to encrypt.
var PASSWORD = 'encryption_password_for_SQLite_and_HTTP_requests';
// Example Base64-encoded encryption payload from an imaginary HTTP request:
var EXAMPLE_BASE_64_DATA = "AwGOqZarBu3DAnVElrdORXPWrFOWeNTufiq2p9WQoXYIx0DAZIyvF5mG2wgzpFwQF6I6hNl0qESY+N0h6oKfw94HgRYzQ0YHHCHvHFzZOyP+lCdoMEW1z1wEGlBj47VPQXC6PHC+LCMh80SwjrQDmxqkoUBaOTK59rGcKU+2gjtmUcZeaZUlY6O4ZWSJ6LZH2YLUFEX/6i3rycZZZwF4z9sFzUV/4mCYzX31JecrbDD/vef0/PY4th3D8QynR3v8eSh8P/Ly5FdVbsK4V1Yje3LlbzhUYgwYez2uxjsQhc0w5K3WbidE85X9PXBvC53sN4I4btyaOtCc+AQA6SxUMsjHvaOoMIjMVJXlIAnRtTE0g0AnW4bDIe3Vgj+USKItlYwhMKoXA+NPIHFE7bcoPLwgBDuUaAJ1gg+x4oXiBH1H517ZgVhODRBHcbh2ETRdrdbqZPN5wihTTncbwXnc4uxoY57KRn3xFe4VgBqKOPvvOFLH1JCR+cE8c9N+OiX5EWBaq3hsl/BVeu8dscwPSNFjniadC9nW80p3j7yVvGt2L6zp6iRmz2CIfocuTIdxwF06zdUyGAyHhoS+D5kDKEryAZPoxNSMU+MXFVz6QBZ3bbvLQZjqh0V8hgf0nD8plneYFlowYWWGL7jg9C3ObY8h3noUNO8YPOuRKAFFlW9k+sk1vQkByhskxuFdqCvGfkVXKYeo8WHAo3O0jCeaAhiFAaBKb0392uClAmp/+z5vyu1OfR51cmw3T4XqNjehWdI=";
// Decode base64 data from HTTP body.
var buffer = Buffer.from(EXAMPLE_BASE_64_DATA, 'base64');
// Byte 0 is version of encryption spec (3)
var version = buffer[0];
// Byte 1 is options (1 = password encryption)
var options = buffer[1];
// Bytes 2-9 is Password Salt
var passwordSalt = buffer.slice(2, 10);
// Bytes 10-17 is HMAC Salt
var hmacSalt = buffer.slice(10, 18);
// Bytes 18-33 is Initialization Vector.
var iv = buffer.slice(18, 34);
// Bytes 34-(n-32) is the actual encrypted data.
var cipher = buffer.slice(34, buffer.length-32);
// Generate pbkdf2 encryption key using password and password salt
var encryptionKey = crypto.pbkdf2Sync(PASSWORD, passwordSalt, 10000, 32, 'sha1');
console.log('*********************************************');
console.log('* version: ', version, ', options: ', options, ', length: ', buffer.length);
console.log('* passwordSalt: ', passwordSalt.length, passwordSalt);
console.log('* hmacSalt: ', hmacSalt.length, hmacSalt);
console.log('* iv: ', iv.length, iv);
console.log('* cipher: ', cipher.length, cipher);
console.log('* encryptionKey: ', encryptionKey.length, encryptionKey);
console.log('*********************************************');
// Create aes-256-cbc Decryption instance.
var decipher = crypto.createDecipheriv('aes-256-cbc', encryptionKey, iv);
try {
// Decrypt the binary data in cipher
var json = decipher.update(cipher, 'binary', 'utf-8');
json += decipher.final('utf-8');
// Parse the JSON into a Location object.
let location = JSON.parse(json);
console.log('[decrypted]', location);
} catch(e) {
console.log('ERROR: ', e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment