Skip to content

Commit 10b8521

Browse files
author
Deepal Jayasekara
committed
multiple changes
- add support for hex output of decryption - add coveralls support to generate automatic test coverage information - add documentation
1 parent 0142d5c commit 10b8521

File tree

10 files changed

+171
-7
lines changed

10 files changed

+171
-7
lines changed

.eslintignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
dist/
2+
node_modules/
3+
test/
4+
gulpfile.js

.eslintrc

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"extends": "eslint-config-airbnb",
3+
"globals": {
4+
"Buffer": true,
5+
"global": true,
6+
"__dirname": true,
7+
"process": true,
8+
"it": true,
9+
"module": true,
10+
"describe": true,
11+
"require": true
12+
},
13+
"rules": {
14+
"indent": [
15+
"error",
16+
4
17+
],
18+
"comma-dangle": [
19+
"error",
20+
"never"
21+
],
22+
"import/no-extraneous-dependencies": [
23+
"error",
24+
{
25+
"devDependencies": true,
26+
"optionalDependencies": false,
27+
"peerDependencies": false
28+
}
29+
],
30+
"no-underscore-dangle": 0,
31+
"no-bitwise": 0
32+
}
33+
}

README.md

Lines changed: 102 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,106 @@
11
# node-dukpt
22

3-
Node Library to provide Derived Unique Key Per Transaction (DUKPT) Encryption
3+
[![npm version](https://badge.fury.io/js/dukpt.svg)](https://badge.fury.io/js/dukpt) ![alt tag](https://api.travis-ci.org/dpjayasekara/node-dukpt.svg?branch=master) ![alt dependencies](https://david-dm.org/dpjayasekara/node-dukpt.svg) ![alt dev-dependencies](https://david-dm.org/dpjayasekara/node-dukpt/dev-status.svg) [![Coverage Status](https://coveralls.io/repos/github/dpjayasekara/node-dukpt/badge.svg)](https://coveralls.io/github/dpjayasekara/node-dukpt) [![Code Climate](https://codeclimate.com/github/dpjayasekara/node-dukpt/badges/gpa.svg)](https://codeclimate.com/github/dpjayasekara/node-dukpt)
4+
5+
6+
##Derived Unique Key Per Transaction (DUKPT) Encryption with NodeJS
7+
8+
This the NodeJS implementation of DUKPT based on the vanilla javascript implementation of **IDTech** DUKPT encryption/decryption. Don't hesitate to report any bugs in the [Github Repository!](https://github.com/dpjayasekara/node-dukpt).
9+
10+
Many thanks to @jamiesoncj for providing resources.
11+
12+
### Installing
13+
14+
```
15+
npm install dukpt --save
16+
```
17+
### Using DUKPT
18+
19+
Initialize DUKPT by providing BDK and KSN:
20+
21+
```
22+
const Dukpt = require('dukpt');
23+
24+
const encryptionBDK = '0123456789ABCDEFFEDCBA9876543210;
25+
const ksn = 'FFFF9876543210E00008';
26+
const encryptedCardData = '411D405D7DEDB9D84797F04<redacted_for_brevity>050509277E5F80BE67A2C324900A7E3';
27+
const plainTextCardData = '%B5452310551227189^DOE/JOHN ^08043210000000725000000?';
28+
29+
const dukpt = new Dukpt(encryptionBDK, ksn);
30+
```
31+
After initializing, you can use `dukptEncrypt` and `dukptDecrypt` methods to encrypt/decrypt data using DUKPT.
32+
33+
#### Encrypting `ascii` data
34+
35+
```
36+
const options = {
37+
inputEncoding: 'ascii',
38+
outputEncoding: 'hex',
39+
encryptionMode: '3DES'
40+
};
41+
const encryptedCardData = dukpt.dukptEncrypt(plainTextCardData, options);
42+
```
43+
44+
#### Encrypting `hex` data
45+
46+
```
47+
const options = {
48+
inputEncoding: 'hex',
49+
outputEncoding: 'hex',
50+
encryptionMode: '3DES'
51+
};
52+
const encryptedCardData = dukpt.dukptEncrypt(plainTextCardData, options);
53+
```
54+
55+
#### Decrypting data with `ascii` output encoding
56+
57+
```
58+
const options = {
59+
outputEncoding: 'ascii',
60+
decryptionMode: '3DES',
61+
trimOutput: true
62+
};
63+
64+
const decryptedCardData = dukpt.dukptDecrypt(encryptedCardData, options);
65+
```
66+
#### Decrypting data with `hex` output encoding
67+
68+
```
69+
const options = {
70+
outputEncoding: 'hex',
71+
decryptionMode: '3DES',
72+
trimOutput: true
73+
};
74+
75+
const decryptedCardData = dukpt.dukptDecrypt(encryptedCardData, options);
76+
```
77+
78+
###Options
79+
80+
You can use options object to provide additional options for the DUKPT encryption/decryption. This object is **optional** and, if you don't provide it, encryption/decryption will use the default values shipped with it.
81+
82+
Following listed are the available options.
83+
84+
Option | Possible Values | Default Value | Description
85+
------------ | ------- | ------------- | --------------
86+
`outputEncoding` | `ascii`, `hex` | For encryption `hex`, for decryption `ascii` | Specify output encoding of encryption/decryption
87+
`inputEncoding` | `ascii`, `hex` | For encryption `ascii`, for decryption `hex` | Specify encoding of the input data for encryption/decryption
88+
`trimOutput` (for decryption only) | `true`, `false` | `false` | Specify whether to strip out null characters from the decrypted output
89+
`encryptionMode` (for encryption only) | `3DES` | `3DES` | Specify encryption scheme for dukpt
90+
`decryptionMode` (for decryption only) | `3DES` | `3DES` | Specify decryption scheme for dukpt
91+
92+
* Support for AES encryption/decryption mode will be added soon!
93+
94+
###Tests
95+
Tests can be run using gulp as follows:
96+
97+
```
98+
gulp test
99+
```
100+
101+
####Roadmap
102+
103+
- [x] Support for DUKPT Encryption/Decryption with 3DES
104+
- [ ] Support for DUKPT Encryption/Decryption with AES
4105

5-
![alt tag](https://api.travis-ci.org/dpjayasekara/node-dukpt.png?branch=master) ![alt dependencies](https://david-dm.org/dpjayasekara/node-dukpt.svg) ![alt dev-dependencies](https://david-dm.org/dpjayasekara/node-dukpt/dev-status.svg) [![Coverage Status](https://coveralls.io/repos/github/dpjayasekara/node-dukpt/badge.svg)](https://coveralls.io/github/dpjayasekara/node-dukpt)
6106

7-
Coming Soon...

dist/gulpfile.js

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/gulpfile.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/lib/dukpt.lib.js

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/lib/dukpt.lib.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gulpfile.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const gulp = require('gulp');
22
const eslint = require('gulp-eslint');
33
const mocha = require('gulp-mocha');
44
const istanbul = require('gulp-istanbul');
5+
const coveralls = require('gulp-coveralls');
56

67
function lint() {
78
return gulp.src(['./index.js', './lib/*.js'])
@@ -17,7 +18,7 @@ function pretest() {
1718
}
1819

1920
function test() {
20-
return gulp.src(['./test/**/*.js'], { read: false })
21+
gulp.src(['./test/**/*.js'], { read: false })
2122
.pipe(mocha())
2223
.pipe(istanbul.writeReports({
2324
dir: 'test-coverage/',
@@ -27,6 +28,8 @@ function test() {
2728
reporters: ['lcov', 'text', 'text-summary', 'cobertura']
2829
}))
2930
.pipe(istanbul.enforceThresholds({ thresholds: { global: 1 } }));
31+
return gulp.src('test-coverage/lcov.info')
32+
.pipe(coveralls());
3033
}
3134

3235
function watch() {

lib/dukpt.lib.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,17 @@ class Dukpt {
235235
return DataOperations.removeNullCharsFromAscii(decryptedOutput);
236236
}
237237

238+
switch (options.outputEncoding.toLowerCase()){
239+
case 'ascii':
240+
// do nothing
241+
break;
242+
case 'hex':
243+
decryptedOutput = DataOperations.dataToHexstring(decryptedOutput);
244+
break;
245+
default:
246+
throw new Error('unsupported output encoding for dukpt decrypt');
247+
}
248+
238249
return decryptedOutput;
239250
}
240251

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"eslint-plugin-jsx-a11y": "2.2.3",
3232
"eslint-plugin-react": "6.8.0",
3333
"gulp": "3.9.1",
34+
"gulp-coveralls": "0.1.4",
3435
"gulp-eslint": "3.0.1",
3536
"gulp-istanbul": "1.1.1",
3637
"gulp-mocha": "3.0.1",

0 commit comments

Comments
 (0)