Skip to content

Commit

Permalink
First
Browse files Browse the repository at this point in the history
  • Loading branch information
emaincourt committed Jan 7, 2017
0 parents commit d4c9ed1
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
###NodeJS wrapper for LibNFC and MFOC
***
LibNFC and MFOC NodeJS wrapper to read/write hex dumps from/to ISO/IEC 14443 A compliant tags.
130 changes: 130 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
const spawn = require('child_process').spawn,
execFile = require('child_process').execFile,
fs = require('fs'),
console = require('better-console'),
colors = require('colors/safe');

var re = /(.{2} .{2} .{2} .{2})/,
error = { 'type' : 0, 'message' : '' };

// Read Mifare Classic 1K UID
// Returns new promise :
// - resolve(uid)
// - reject({error.type,error.message})
function readMifareUID() {

return new Promise(function(resolve,reject){

execFile('nfc-list', (err, stdout, stderr) => {

error.message = stderr || "";
error.message = err || "";

error.type = 0;

if(err || stderr)
reject(error);
else
resolve(extractUIDFromOutput(stdout));

});

});

}

// Prints Hex Data File to terminal
// Input : path to the file, cb Callback function to execute after reading file
// Output : null
function readHexFile(path,cb){
fs.readFile(path, function(err, code){

var buffer = [[]],
index = 0;

code.forEach(function(el){

buffer[index].push(el.toString(16));

if(buffer[index].length == 16){
buffer.push([]);
index++;
}
});

console.table(buffer);

if(cb && typeof cb === 'function')
cb(buffer);

});
}

// Reads Mifare Classic 1K tag and writes dump to path
// Input : Path to the file
// Output : Promise that will be resolved after reading created file
function readMifareClassic(path){
return new Promise(function(resolve,reject){
writeMifareDumpToFile(path,resolve,reject).then(function(path){
readHexFile(path);
resolve();
},function(err){
reject(err);
});
});
}

// Write Mifare Dumb to file
// Input : Path to the file
// Output : Promise that will be resolved when file is written
function writeMifareDumpToFile(path){
return new Promise(function(resolve,reject){

console.log(colors.blue('Please wait while trying to authenticate to the tag...'));

execFile('mfoc', ['-O' + path].concat(keys), (err, stdout, stderr) => {

error.message = stderr || "";
error.message = err || "";

error.type = 1;

if(err || stderr)
reject(error);
else
resolve(path);

});
});
}

function cloneMifareClassic(source,target,unlock,callback){

var params = (unlock) ? ' W X ' : ' w x ';
params += target + ' ' + source;

execFile('nfc-mfclassic' + params, [], (err, stdout, stderr) => {

error.message = stderr || "";
error.message = err || "";

error.type = 2;

if(err || stderr)
console.log(error.message);
else
console.log(colors.green('Successfully cloned.'));

})
}

function extractUIDFromOutput(output){
return (output.match(re)) ? output.match(re)[1].replace(/ /g,'') : 0;
}

module.exports = {
readMifareUID: readMifareUID,
readMifareClassic: readMifareClassic,
readHexFile : readHexFile,
writeMifareDumpToFile: writeMifareDumpToFile
};
18 changes: 18 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "mifare-node-js",
"author": "Elliot Maincourt <e.maincourt@gmail.com>",
"version": "0.0.1",
"description": "Read and write dumps of Mifare Classic 1K tags.",
"main": "index.js",
"keywords": [
"Mifare",
"NodeJS",
"NFC"
],
"readmeFilename": "README.md",
"dependencies": {
"better-console": "^1.0.0",
"colors": "^1.1.2",
"console.table": "^0.8.0"
}
}

0 comments on commit d4c9ed1

Please sign in to comment.