payment-utils
v1.0.0
Published
Algorithmic crypto and payment card tooling derived from paymentcardtools.com
Readme
payment-utils
Algorithmic crypto and payment-card utilities derived from the browser-side logic used by paymentcardtools.com.
This package is intentionally narrow in scope:
- Pure, reusable calculators and generators
- Payment-card cryptography helpers
- Hashing, MAC, PIN, CVV, ARQC, and validation utilities
It does not aim to ship the original website UI, static lookup tables, or CLI wrappers.
Requirements
- Node.js
>=22 - ESM import style
Install
npm install payment-utilsUsage
All public functions are exported from the package root.
import {
calculateCvv,
calculateVisaPvv,
runDesCalculator,
validateLuhnPan,
} from "payment-utils";
const cvv = calculateCvv({
pan: "4761739001010010",
expiry: "2512",
serviceCode: "101",
cvkA: "0123456789ABCDE0",
cvkB: "FEDCBA9876543210",
});
const pvv = calculateVisaPvv({
pan: "4761739001010010",
pin: "1234",
pvki: "1",
pvk: "0123456789ABCDEFFEDCBA9876543210",
});
const encrypted = runDesCalculator({
data: "0123456789ABCDEF",
key: "0123456789ABCDEFFEDCBA9876543210",
operation: "encrypt",
mode: "ecb",
});
const isValid = validateLuhnPan("4761739001010010");Conventions
- Hex inputs are expected as hexadecimal strings without separators.
- Numeric inputs such as PANs, service codes, dates, and amounts are expected as digit strings unless noted otherwise.
- Functions throw
Errorwhen an input has an invalid length, invalid characters, or unsupported option values. - Unless a function explicitly returns an object, it returns a string.
Available Tools
Symmetric Crypto
runAesCalculator(options)
Encrypts or decrypts AES data in ECB or CBC mode.
Options:
data: hex stringkey: hex stringoperation:"encrypt"or"decrypt"; default"encrypt"mode:"ecb"or"cbc"; default"ecb"paddingMethod:1or2; default1iv: 16-byte hex IV for CBC; default00000000000000000000000000000000
Returns:
- Hex string
Notes:
- Encryption pads using ISO 9797 method 1 or 2.
- Decryption requires a non-empty multiple of 16 bytes.
- When decrypting with
paddingMethod: 2, trailing ISO 9797 method 2 padding is stripped from the returned value.
runDesCalculator(options)
Encrypts or decrypts DES / 3DES-style block data in ECB or CBC mode.
Options:
data: hex stringkey: hex stringoperation:"encrypt"or"decrypt"; default"encrypt"mode:"ecb"or"cbc"; default"ecb"paddingMethod:1or2; default1iv: 8-byte hex IV for CBC; default0000000000000000
Returns:
- Hex string
Notes:
- Encryption pads using ISO 9797 method 1 or 2.
- Decryption requires a non-empty multiple of 8 bytes.
- When decrypting with
paddingMethod: 2, trailing ISO 9797 method 2 padding is stripped from the returned value.
runBitwiseCalculator(options)
Performs small bitwise operations on hex strings.
Options:
operation:"xor","and","or", or"not"dataA: hex string, up to 8 bytesdataB: hex string, required for"xor","and", and"or", and must matchdataAlength
Returns:
- Hex string
runBase64Calculator(options)
Encodes or decodes Base64 data.
Options:
data: input stringoperation:"encode"or"decode"; default"encode"mode:"text"or"hex"; default"text"
Returns:
- String
Notes:
- In
"text"mode, input or output is treated as UTF-8 text. - In
"hex"mode, input or output is treated as hexadecimal data.
Hashing And MAC
calculateSha1(options)
Computes a SHA-1 digest.
Options:
data: input stringmode:"text"or"hex"; default"text"
Returns:
- Uppercase hex digest string
calculateSha256(options)
Computes a SHA-256 digest.
Options:
data: input stringmode:"text"or"hex"; default"text"
Returns:
- Uppercase hex digest string
calculateHmac(options)
Computes an HMAC digest.
Options:
data: input stringkey: input stringalgorithm:"sha1"or"sha256"; default"sha1"mode:"text"or"hex"; default"text"
Returns:
- Uppercase hex digest string
calculateMacIso9797Alg3(options)
Computes an ISO 9797-1 Algorithm 3 MAC using a double-length key.
Options:
data: hex stringkey: 16-byte hex keypaddingMethod:1or2; default1
Returns:
- 8-byte MAC as a hex string
Card Security Values
calculateCvv(options)
Calculates a card verification value from PAN, expiry, service code, and CVKs.
Options:
pan: 13 to 19 digitsexpiry: 4 digits inYYMMserviceCode: 3 digitscvkA: 8-byte hex keycvkB: 8-byte hex key
Returns:
- 3-digit CVV string
calculateDcvv(options)
Calculates dynamic CVV using an ATC and MDK.
Options:
pan: 13 to 19 digitsexpiry: 4 digits inYYMMserviceCode: 3 digitsatc: 2-byte hex transaction countermdk: 16-byte hex master derivation key
Returns:
- 3-digit dCVV string
calculateCavv(options)
Calculates a CAVV v7 payload and returns both hex and Base64 forms.
Options:
pan: 16 digitsauthenticationResultsCode: 1 digitauthenticationMethod: 2 digitskeySet: 2 digitseci: 2 digitsseed: 2-byte hex valuetransactionDate: date-like value accepted by the package date formatteramount: 12 digitscurrencyCode: 3 digitsprotocolVersion: 1 digitkey: 16-byte hex key
Returns:
{
hex: "....",
base64: "...."
}calculateSpaAavCvc2(options)
Calculates the SPA AAV / CVC2 value and returns the Base64-encoded payload.
Options:
merchantName: stringpan: 16 digitsmode: SPA mode accepted by the upstream normalization logicacsId: 1 to 2 digitsauthenticationMethod: 1 digitsequenceNumber: 4-byte hex valuekeyIndex: 1 to 2 digitskey: 16-byte hex key
Returns:
- Base64 string
PIN Tools
buildIso9564Format0PinBlock(options)
Builds an ISO 9564 format 0 clear PIN block.
Options:
pan: 13 to 19 digitspin: 4 to 12 digits
Returns:
- 8-byte hex PIN block
isLuhnOptionalWarning(pan)
Checks whether the supplied PAN fails Luhn validation. This mirrors the UI behavior used to show an optional warning, not a hard failure.
Arguments:
pan: 13 to 19 digits
Returns:
- Boolean
buildIso9564Format1PinBlock(options)
Builds an ISO 9564 format 1 clear PIN block.
Options:
pin: 4 to 12 digits
Returns:
- 8-byte hex PIN block
extractPinFromPinBlock(options)
Extracts a PIN from a clear or encrypted PIN block.
Options:
pinBlock: 8-byte hex PIN blockpan: 13 to 19 digitspek: optional 16-byte hex PIN encryption key; when present, the block is decrypted before parsing
Returns:
{
pin: "1234",
format: "0",
clearPinBlock: "0412FFFFFFFFFFFF"
}Notes:
- Supports format
0and1. - Throws if the format nibble is unsupported or the block padding is invalid.
calculateVisaPvv(options)
Calculates a Visa PVV.
Options:
pan: 13 to 19 digitspin: 4 to 12 digitspvki: 1 digitpvk: 16-byte hex key
Returns:
- 4-digit PVV string
findPinsForPvv(options)
Brute-forces all 4-digit PIN values that match a target PVV.
Options:
pvv: 4 digitspan: 13 to 19 digitspvki: 1 digitpvk: 16-byte hex key
Returns:
- Array of matching 4-digit PIN strings
calculateIbm3624Pin(options)
Calculates an IBM 3624 natural PIN.
Options:
pan: 13 to 19 digitsdecimalizationTable: 16 digitspinLength: PIN length used to truncate the natural PINpdk: 8-byte hex PIN derivation key
Returns:
- PIN string
calculateIbm3624PinOffset(options)
Calculates an IBM 3624 PIN offset.
Options:
intermediatePin: digit stringcustomerSelectedPin: digit string
Returns:
- Offset string
EMV And Payment Cryptogram Tools
deriveUdk(options)
Derives a unique derived key from a master key and PAN data.
Options:
masterKey: 16-byte hex keypan: 13 to 19 digitspanSequenceNumber: 1-byte hex valuemethod: derivation method; default"a"parity: key parity mode; default"odd"
Returns:
- 16-byte hex derived key
calculateArqcCvn10(options)
Calculates Visa CVN 10 ARQC and ARPC values.
Options:
pan: 16 digitspanSequenceNumber: 1 digit; default"0"amountAuthorized: 12 digitsamountOther: 12 digitscountryCode: 3 digitstvr: 5-byte hex TVRcurrencyCode: 3 digitstransactionDate: date-like value accepted by the package date formattertransactionType: 2 digitsunpredictableNumber: 4-byte hex valueaip: 2-byte hex valueatc: 2-byte hex valuecvr: 4-byte hex valueresponseCode: response code consumed by the ARPC generation logicimk: 16-byte hex issuer master key
Returns:
{
arqc: "....",
arpc: "...."
}calculateArqcCvn18(options)
Calculates Visa CVN 18 ARQC and ARPC values.
Options:
pan: 16 digitspanSequenceNumber: 1 digit; default"0"amountAuthorized: 12 digitsamountOther: 12 digitscountryCode: 3 digitstvr: 5-byte hex TVRcurrencyCode: 3 digitstransactionDate: date-like value accepted by the package date formattertransactionType: 2 digitsunpredictableNumber: 4-byte hex valueaip: 2-byte hex valueatc: 2-byte hex valueiad: 7 to 32 bytes of hex datacsu: 4-byte hex valueimk: 16-byte hex issuer master key
Returns:
{
arqc: "....",
arpc: "...."
}calculateArqcMchip(options)
Calculates Mastercard M/Chip ARQC and ARPC values for supported application versions.
Options:
pan: 16 to 19 digitspanSequenceNumber: 1 digit; default"0"amountAuthorized: 12 digitsamountOther: 12 digitscountryCode: 3 digitstvr: 5-byte hex TVRcurrencyCode: 3 digitstransactionDate: date-like value accepted by the package date formattertransactionType: 2 digitsunpredictableNumber: 4-byte hex valueaip: 2-byte hex valueatc: 2-byte hex valuecvr: 6-byte hex valuearpcResponseCode: 2-byte hex valueversion:"16","17","20", or"21"; default"16"imk: 16-byte hex issuer master keycumulativeOfflineTransactionAmount: required for versions"17"and"21"; 12 digitsconsecutiveOfflineTransactionsNumber: required for versions"17"and"21"; 1 to 2 digits
Returns:
{
arqc: "....",
arpc: "...."
}Validation
validateLuhnPan(pan)
Validates a PAN with the Luhn algorithm.
Arguments:
pan: 13 to 19 digits
Returns:
- Boolean
generateLuhnCheckDigit(panWithoutCheckDigit)
Generates the Luhn check digit for a numeric payload.
Arguments:
panWithoutCheckDigit: digit string
Returns:
- Single-digit string
Development
Run the test suite with:
npm testThe repository also includes:
datasets/: captured tool fixtures used by teststest/: dataset-backed automated testsupstream/js/: mirrored upstream browser scripts used as implementation references
License
MIT
