auto-account-machine/docs/tools/card-generator.md
dengqichen 540423f2bf aaaaa
2025-11-16 19:12:44 +08:00

232 lines
4.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 信用卡生成器 (Card Generator)
虚拟信用卡号生成工具,支持多种卡类型和输出格式。
## 功能特性
- ✅ 支持多种国际卡类型银联、Visa、MasterCard、AMEX、Discover
- ✅ 可选 Luhn 算法校验
- ✅ 灵活的有效期生成2026-2030
- ✅ 多种输出格式Pipe、JSON、CSV、Pretty
- ✅ 批量生成支持
## 技术实现
### 卡号生成算法
#### 纯随机生成
```
卡号 = 固定前缀 + 随机数字
```
#### Luhn算法生成
```
1. 固定前缀 + 随机数字n-1位
2. 计算校验位
3. 附加校验位得到完整卡号
```
### 有效期生成
```javascript
月份: random(1-12) 格式化为两位数 "01"-"12"
年份: random(26-30) 格式化为两位数 "26"-"30"
```
### CVV生成
```javascript
CVV = random(0, 10^cvvLength - 1) 格式化为指定位数
```
## 配置说明
### 卡类型配置结构
```javascript
{
name: '卡类型名称',
prefix: '卡号前缀',
length: 卡号总长度,
cvvLength: CVV长度,
useLuhn: 是否使用Luhn算法
}
```
### 添加新卡类型
编辑 `src/tools/card-generator/config.js`
```javascript
CARD_TYPES.newcard = {
name: '新卡类型',
prefix: '123456',
length: 16,
cvvLength: 3,
useLuhn: true
};
```
## API 参考
### CardGenerator 类
```javascript
const CardGenerator = require('./generator');
const generator = new CardGenerator();
```
#### 方法
**generate(type)**
- 参数:`type` (string) - 卡类型,默认 'unionpay'
- 返回Object - 包含 number, month, year, cvv, type
**generateBatch(count, type)**
- 参数:
- `count` (number) - 生成数量
- `type` (string) - 卡类型,默认 'unionpay'
- 返回Array - 卡信息数组
**getSupportedTypes()**
- 返回Array - 所有支持的卡类型
### Formatter 类
```javascript
const Formatter = require('./formatter');
const formatter = new Formatter();
```
#### 方法
**format(card, format)**
- 参数:
- `card` (Object) - 卡信息对象
- `format` (string) - 格式类型,默认 'pipe'
- 返回string - 格式化后的字符串
**formatBatch(cards, format)**
- 参数:
- `cards` (Array) - 卡信息数组
- `format` (string) - 格式类型
- 返回string - 格式化后的字符串(多行)
## 使用示例
### 基础用法
```bash
# 生成单张银联卡
node src/index.js card
# 生成Visa卡
node src/index.js card -t visa
# 批量生成
node src/index.js card -n 10
```
### 不同格式输出
```bash
# Pipe格式默认
node src/index.js card
# 输出: 6228367541234567|08|28|456
# JSON格式
node src/index.js card -f json
# CSV格式
node src/index.js card -f csv
# 美化格式
node src/index.js card -f pretty
```
### 编程使用
```javascript
const CardGenerator = require('./src/tools/card-generator/generator');
const Formatter = require('./src/tools/card-generator/formatter');
const generator = new CardGenerator();
const formatter = new Formatter();
// 生成单张卡
const card = generator.generate('visa');
console.log(formatter.format(card, 'pipe'));
// 批量生成
const cards = generator.generateBatch(5, 'unionpay');
cards.forEach(card => {
console.log(formatter.format(card, 'pipe'));
});
```
## 测试验证
### 手动测试
```bash
# 测试不同卡类型
node src/index.js card -t unionpay
node src/index.js card -t visa
node src/index.js card -t mastercard
node src/index.js card -t amex
node src/index.js card -t discover
# 测试批量生成
node src/index.js card -n 20
# 测试不同格式
node src/index.js card -f json
node src/index.js card -f csv
node src/index.js card -f pretty
```
### Luhn算法验证
```bash
# 生成Visa卡使用Luhn算法
node src/index.js card -t visa -f json
# 使用在线工具验证卡号
# https://www.freeformatter.com/credit-card-number-generator-validator.html
```
## 常见问题
### Q: 为什么银联卡不使用Luhn算法
A: 可以在配置中启用。默认不启用是为了保持与原始样本数据的一致性。
### Q: 如何修改有效期范围?
A: 编辑 `src/tools/card-generator/config.js` 中的 `EXPIRY_CONFIG`
### Q: 生成的卡号能用于真实交易吗?
A: **绝对不能!** 这些是测试用的虚拟卡号,没有任何实际价值。
### Q: 如何添加自定义输出格式?
A: 在 `config.js``OUTPUT_FORMATS` 中添加新格式:
```javascript
OUTPUT_FORMATS.custom = {
name: '自定义格式',
formatter: (card) => `${card.number} - ${card.month}/${card.year} - ${card.cvv}`
};
```
## 扩展建议
- [ ] 支持自定义前缀和长度
- [ ] 添加卡号验证功能
- [ ] 支持配置文件导入卡类型
- [ ] 生成完整的持卡人信息(姓名、地址等)
- [ ] 支持特定BIN银行识别码生成
## 相关资源
- [Luhn算法详解](https://en.wikipedia.org/wiki/Luhn_algorithm)
- [信用卡号结构](https://en.wikipedia.org/wiki/Payment_card_number)
- [BIN数据库](https://binlist.net/)