低配版密码生成代码

低配版密码生成代码

package com.wifi.util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Maple.Chen
 * @date 2020年10月31日
 * @description <p>用于生成密码字典的工具类</p>
 * @description <p>Tool class for generating password dictionaries</p>
 */

public class PaswGenerateUtil {
	
	// 定义枚举值类型
	public static final String Log_Oprate_Append = "Append";
	public static final String Log_Oprate_Overlay = "Overlay";
	
	// 定义日志文件的名称及输出路径
	public static final String LOG_GENERATE_PATH = "C:" + File.separator + "Users" + File.separator + "bigTiger" + File.separator + "Desktop" + File.separator + "dictionary.txt";
	/*
	 * 密码中的基本字符,可自定义
	 * (Basic characters in passwords, customizable)
	 */
//	public static String[] baseWord = {
//			"1","2","3","4","5","6","7","8","9","0"
//	};
	public static String[] baseWord = {
			"1","2","3","4","5","6","7","8","9","0",
			"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
			"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
			"!","@","#","$","%","^","&","*","(",")","-","+","=",";",":","?","<",">","/",",","'","\"","{","}","[","]","_"
	};
	/*
	 * 一个用于存储密码的长字符串,它的内容将被写入到密码字典
	 * (A long string for storing passwords whose contents are written to the password dictionary)
	 */
	private static List<String> passwordList = new ArrayList<>();
	// 一个累加器,密码数量到达一万,即输出到字典文件中 (An accumulator is exported to a dictionary with a password of 10,000)
	
	private static int counter = 0;
	private static int counter_for_w = 0;
	/*
	 * 调用递归算法生成密码
	 * (Call recursive algorithm to generate password)
	 */
	public static void passwordGenerator(String initStr, int targetMinLength, int targetMaxLength, int nowLength){
		if ( nowLength + 1 > targetMaxLength )
			return;
		/*
		 * 对密码中的第n位码选取算子
		 */
		String tempStr;
		int tempLength;
		for ( String arithmeticOperator : baseWord ){
			// 当前递归项算子与当前递归初始入参拼接得到的密码串
			tempStr = initStr + arithmeticOperator;
			// 当前密码串长度加一
			tempLength = nowLength + 1;
			// 判断密码长度否在密码期望长度内,是就记录密码
			if ( tempLength >= targetMinLength && tempLength <= targetMaxLength  ){
				counter += 1;
				passwordList.add(tempStr);
				// 若在生成密码的途中结果数数大于10000,则按1w分批存储。否则在方法执行完后从main入口处统一存储
				if ( counter == 10000 ){
					logFileWriter(
							passwordList.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining("\n")),
							LOG_GENERATE_PATH,
							Log_Oprate_Append
							);
					counter = 0;
					passwordList = new ArrayList<>();
					counter_for_w += 1;
					System.out.println("+1w (" + counter_for_w + ")");
				}
			}
			// 递归生成下一轮密码
			passwordGenerator(tempStr, targetMinLength, targetMaxLength, tempLength);
		}
	}
	
	public static void main(String[] args) {
		logFileWriter(
				"",
				LOG_GENERATE_PATH,
				Log_Oprate_Overlay
				);
		long startTime = System.currentTimeMillis();
		/**
		 * 若baseWord有k个算子,需要生成一本每个密码串长度在[n,m]∈N的密码字典
		 * 则字典中的密码数为n^k+(n+1)^k+...+m^k
		 */
//		passwordGenerator("", 6, 6, 0);
		passwordGenerator("", 8, 13, 0);
		long stopTime = System.currentTimeMillis();
		logFileWriter(
				passwordList.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining("\n")),
				LOG_GENERATE_PATH,
				Log_Oprate_Append
				);
		System.out.println("生成字典耗时:" +(stopTime - startTime) + "ms");
		
		/**
		 * 生成约3亿密码的密码本txt,大约需要3.91Gbit的Space
		 * 单线程验证一个密码需要约30ms,大约一分钟单线程可以验证2000个密码,根据计算机的浮点运算能力有上下波动差
		 * 十线程每分钟可验证2w个密码,
		 * ---------------------
		 * 由89个字符排序组成的密码,8位的共有89^8个
		 */
	}
	
	
	/**
	 * 密码本文件处理
	 */
	public static void logFileWriter(String logInfo, String filePath, String type) {
		FileWriter fw = null;
		try {
			
			// 1.提供File类的对象,指明写出到的文件(若只写文件名,则生成到工程目录下)
			File file = new File( filePath );

			// 2.提供FileWriter的对象,用于数据的写出;第二个参数判断是否在文件内容追加
			/**
			 * 第二个参数为true:	则是每次向文件中追加内容
			 * 第二个参数为false:则是每次覆写文件中的内容
			 */
			switch (type) {
				case Log_Oprate_Append:
					fw = new FileWriter(file, true);
					break;
				case Log_Oprate_Overlay:
					fw = new FileWriter(file, false);
					break;
				default: 
					break;
			}
			// 3.写出的操作
			fw.write(logInfo);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 4.流资源的关闭
			if (fw != null) {
				try {
					fw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值