make Password - Java Security

Java examples for Security:Password

Description

make Password

Demo Code


//package com.java2s;
import java.security.MessageDigest;

public class Main {
    public static void main(String[] argv) throws Exception {
        String password = "java2s.com";
        System.out.println(makePassword(password));
    }//from   www. j  a va 2s . c o m

    public static String makePassword(String password) {
        return md5(password, 14);
    }

    public static String md5(String str) {
        MessageDigest messageDigest = null;
        try {
            messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.reset();
            messageDigest.update((str).getBytes("utf-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        assert messageDigest != null;
        byte[] byteArray = messageDigest.digest();
        StringBuilder md5StrBuff = new StringBuilder();
        for (byte aByteArray : byteArray) {
            if (Integer.toHexString(0xFF & aByteArray).length() == 1) {
                md5StrBuff.append("0").append(
                        Integer.toHexString(0xFF & aByteArray));
            } else {
                md5StrBuff.append(Integer.toHexString(0xFF & aByteArray));
            }
        }
        return md5StrBuff.toString().replace("-", "").toLowerCase();
    }

    public static String md5(String str, int time) {
        for (int i = 0; i < time; i++) {
            str = md5(str);
        }
        return str;
    }
}

Related Tutorials