随机密码生成器

释放双眼,带上耳机,听听看~!

首先这……不能称之为项目,没什么难度。因为自己的懒惰,已经懒得想每个账号的密码了,我基本上记不住我所有账号的密码,把每一个密码整成一样的又感觉不太安全,一个凉全部凉……于是,干脆打算写个随机密码,这样就连我自己都不知道密码是什么了,只要找个安全的地方存着ctrl+F一下就好。

起笔时想到Java中祖传的random函数,想了想还是算了。不太想使用random函数。我使用了系统时间+哈希随机值混合计算的规则来获取索引,从而获取字符。不得不说,我是一个懒惰的coder……

写个工具类——存字符

package d01_password;

public class Tool {
    public static String []char_str_low ={"a","b","c","d","e","f","g","h","i","j",
            "k","l","n","m","o","p","q","r","s","t","u","v","w","x","y","z"} ;
    /*public static String []char_str_up ={"a","b","c","d","e","f","g","h","i","j",
            "k","l","n","m","o","p","q","r","s","t","u","v","w","x","y","z"} ;*/
    public static String []char_num = {"0","1","2","3","4","5","6","7","8","9"};
    public static String []chr = {"!","@","#",",",".","?","(",")","&","!","!",};

}

写个规则类

package d01_password;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Password {
    public static String password = "";
    public static int password_length = 12;
    public Tool t = new Tool();

    public String Create_Password() {
        SimpleDateFormat sdf_s = new SimpleDateFormat("ss");
        SimpleDateFormat sdf_m = new SimpleDateFormat("mm");
        SimpleDateFormat sdf_H = new SimpleDateFormat("HH");
        int hash_time_ss = sdf_s.format(new Date()).toString().hashCode();
        int hash_time_mm = sdf_m.format(new Date()).toString().hashCode();
        int hash_time_HH = sdf_H.format(new Date()).toString().hashCode();


        for(int i =0;i<password_length;i++) {

            int result_str_index = (hash_time_ss + hash_time_HH + i)%26;
            int result_num_index = (hash_time_mm - hash_time_HH + hash_time_ss + i)%10;
            int result_chr_index = (hash_time_ss + hash_time_mm + i)%8;
    
            int randon_index = result_str_index + result_num_index + result_chr_index;
    
            if(randon_index%10<3) {
                password = password + t.char_str_low[result_str_index];
            }else if(randon_index%10>=3 &&  randon_index%10<6) {
                password = password + t.char_num[result_num_index];
            }else if(randon_index%10>=6 &&  randon_index%10<7){
                password = password + t.chr[result_chr_index];
            }else {
                password = password + t.char_str_low[result_str_index].toUpperCase();
            }
        }
        return password;
    }
}

写个主类

package d01_password;

import java.text.FieldPosition;
import java.text.Format;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;

public class mainTest {

    public static void main(String[] args) {
        Password p = new Password();
        String password = p.Create_Password();

    }

}

寥寥草草就跑起来就行了……结果就是每一秒获取到的密码都不相同。

在写这个随机获取的时候,其实有一点我也是没考虑到的,就是运行一行代码的时间,可能只有几毫秒,甚至微秒,这时候如果不增加其他的规则,那么结果将会是全相同的字符。

将页面部署到了服务器,这下可以方便用了。

人已赞赏
知识分享

存储单位与带宽单位分析

2020-9-19 22:33:00

知识分享

RequestDisapacher接口

2020-9-19 22:36:00

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索