Java随机生成一个登录密码

采用BCrypt

/**
 * 随机生成一个密码
 *
 * 引入jar包
 *         <dependency>
 *             <groupId>org.apache.commons</groupId>
 *             <artifactId>commons-lang3</artifactId>
 *             <version>3.10</version>
 *         </dependency>
 *
 *         <dependency>
 *             <groupId>org.mindrot</groupId>
 *             <artifactId>jbcrypt</artifactId>
 *             <version>0.4</version>
 *         </dependency>
 */
public class RandomPassword {
    public static void main(String[] args) {
        /**
         * 创建一个随机字符串,其长度为指定的字符数。
         * 字符将从一组拉丁字母字符 (az, AZ) 中选择。
         * 参数:count – 要创建的随机字符串的长度
         * 返回:随机字符串
         */
        String plainText = RandomStringUtils.randomAlphabetic(8);
        System.out.println(plainText);

        // 加密密码
        String hashpw = BCrypt.hashpw(plainText, BCrypt.gensalt());

        String inputText = "HHSSNb";
        /**
         * 解密匹配不加密
         * 参数一:用户数据的值
         * 参数二:需要对比的值
         */
        boolean checkpw = BCrypt.checkpw(inputText, hashpw);
        System.out.println(checkpw);
    }
}

版权声明:本文为IOTbaqi原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。