demo.learn.shiro.tool.PasswordMatcherTool.java Source code

Java tutorial

Introduction

Here is the source code for demo.learn.shiro.tool.PasswordMatcherTool.java

Source

/**
 * Copyright 2013 Jee Vang
    
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
    
   http://www.apache.org/licenses/LICENSE-2.0
    
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package demo.learn.shiro.tool;

import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.codec.Base64;
import org.apache.shiro.util.SimpleByteSource;

import demo.learn.shiro.constant.S;

/**
 * Password matcher tool. Used to see if 
 * plain text password matches hashed password
 * given the salt.
 * @author Jee Vang
 *
 */
public class PasswordMatcherTool {

    /**
     * Main method.
     * @param args Pass in plain text password, hashed password,
     * and salt. These arguments are generated from
     * {@link PasswordEncryptionTool}.
     * @throws ParseException
     */
    @SuppressWarnings("static-access")
    public static void main(String[] args) throws ParseException {
        String username = "";
        String plainTextPassword = "root";
        String hashedPasswordBase64 = "ZzIkhapTVzGkhWRQqdUn2zod5npt9RJMSni8My6X+r8=";
        String saltBase64 = "BobnkcsIXcZGksA30eOySA==";
        String realmName = "";

        Option p = OptionBuilder.withArgName("password").hasArg().withDescription("plain text password")
                .isRequired(false).create('p');
        Option h = OptionBuilder.withArgName("password").hasArg().withDescription("hashed password")
                .isRequired(false).create('h');
        Option s = OptionBuilder.withArgName("salt").hasArg().withDescription("salt (Base64 encoded)")
                .isRequired(false).create('s');

        Options options = new Options();
        options.addOption(p);
        options.addOption(h);
        options.addOption(s);

        try {
            CommandLineParser parser = new BasicParser();
            CommandLine cmd = parser.parse(options, args);

            if (cmd.hasOption("p")) {
                plainTextPassword = cmd.getOptionValue("p");
            }
            if (cmd.hasOption("h")) {
                hashedPasswordBase64 = cmd.getOptionValue("h");
            }
            if (cmd.hasOption("s")) {
                saltBase64 = cmd.getOptionValue("s");
            }
        } catch (ParseException pe) {
            String cmdLineSyntax = "java -cp %CLASSPATH% demo.learn.shiro.tool.PasswordMatcherTool";
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp(cmdLineSyntax, options, false);
            return;
        }

        SimpleByteSource salt = new SimpleByteSource(Base64.decode(saltBase64));
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username, hashedPasswordBase64, salt,
                realmName);
        UsernamePasswordToken token = new UsernamePasswordToken(username, plainTextPassword);

        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
        matcher.setHashIterations(S.HASH_ITER);
        matcher.setStoredCredentialsHexEncoded(false);
        matcher.setHashAlgorithmName(S.ALGORITHM_NAME);

        boolean result = matcher.doCredentialsMatch(token, info);
        System.out.println("match? " + result);

    }

}