|
1 | 1 | # node-dukpt |
2 | 2 |
|
3 | | -Node Library to provide Derived Unique Key Per Transaction (DUKPT) Encryption |
| 3 | +[](https://badge.fury.io/js/dukpt)    [](https://coveralls.io/github/dpjayasekara/node-dukpt) [](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 |
4 | 105 |
|
5 | | -   [](https://coveralls.io/github/dpjayasekara/node-dukpt) |
6 | 106 |
|
7 | | -Coming Soon... |
|
0 commit comments