StreampartyLegacyPwdcheck.java 2.26 KB
/*
 * Copyright Codecrew Ry
 * 
 * All rights reserved.
 * 
 * This license applies to any software containing a notice placed by the 
 * copyright holder. Such software is herein referred to as the Software. 
 * This license covers modification, distribution and use of the Software. 
 * 
 * Any distribution and use in source and binary forms, with or without 
 * modification is not permitted without explicit written permission from the 
 * copyright owner. 
 * 
 * A non-exclusive royalty-free right is granted to the copyright owner of the 
 * Software to use, modify and distribute all modifications to the Software in 
 * future versions of the Software. 
 * 
 */
package fi.codecrew.moya.utilities;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StreampartyLegacyPwdcheck {

	private static final Logger logger = LoggerFactory.getLogger(StreampartyLegacyPwdcheck.class);

	private static final String streampartyStringToSha(String password) {
		String paluu = "";
		try {
			MessageDigest algo = MessageDigest.getInstance("SHA");
			algo.update(password.getBytes(), 0, password.length());
			paluu = new BigInteger(1, algo.digest()).toString(16);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return paluu;
	}

	public static boolean streampartyOldAlgoMatch(String plaintext, String encrypted) {
		String salt = encrypted.substring(0, 4);

		logger.debug("Got hash: " + salt);
		return encrypted.equals(salt + streampartyStringToSha(salt + plaintext.trim()));

	}

	public static String oldEncode(String password) {
		try {
			MessageDigest md = MessageDigest.getInstance("SHA");

			return new String(Base64.encodeBase64(md.digest(password.getBytes())), "UTF-8");
			// return Base64.encode(md.digest(password.getBytes()));

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

	}

	public static boolean oldIsMatch(String password, String encrypted) {
		return (encrypted.equals(oldEncode(password)));

	}
}