-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d4c9ed1
Showing
4 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
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
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. |
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
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 | ||
}; |
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
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" | ||
} | ||
} |