check password - Java Security

Java examples for Security:Password

Description

check password

Demo Code


//package com.java2s;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {

    public static boolean checkpassword(String newpasswd, String oldpasswd) {
        if (EncoderByMd5(newpasswd).equals(oldpasswd))
            return true;
        else/*from  w  ww  .ja va2  s .  c  o  m*/
            return false;
    }

    public static String EncoderByMd5(String plainText) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes());
            byte b[] = md.digest();

            int i;

            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }

            //         System.out.println("result: " + buf.toString());// 32?????

            //         System.out.println("result: " + buf.toString().substring(8, 24));// 16?????

            return buf.toString();

        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }

    }
}

Related Tutorials