UglyFix.java 2.53 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 org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UglyFix {

	// Don't touch. Fix old byte to String conversion bugs in passwords
	private static final byte FIXSTR[] = { -17, -65, -67 };

	private final byte[] newarray;
	private final byte[] oldarray;
	private int newint;

	private int oldint;
	private static final Logger logger = LoggerFactory.getLogger(UglyFix.class);

	public UglyFix(String newBase64, String oldBase64) {
		newint = 0;
		oldint = 0;
		newarray = Base64.decodeBase64(newBase64);
		oldarray = Base64.decodeBase64(oldBase64);
	}

	public boolean check() {

		boolean ret = true;
		for (oldint = 0; oldint < oldarray.length && newint < newarray.length; ++oldint) {
			if (oldarray[oldint] != newarray[newint]) {
				logger.debug("No match checking against FIXSTR");

				if (!checkForHITA()) {
					logger.debug("hitacheck not ok. Returning false!");
					ret = false;
					break;
				}
			}
			++newint;

		}
		return ret;
	}

	private boolean checkForHITA() {
		boolean ret = false;

		if (oldint + 3 < oldarray.length &&
				oldarray[oldint] == FIXSTR[0] &&
				oldarray[oldint + 1] == FIXSTR[1] &&
				oldarray[oldint + 2] == FIXSTR[2]) {
			logger.debug("FOUND hita string");
			/*
			 * we have found the "hands in the air string" and thus skipped at
			 * least one newarray character, but maybe more.. So let's check.
			 */
			oldint = oldint + 3;
			++newint;

			for (int testnew = newint; testnew < newarray.length; ++testnew) {
				logger.debug("checking char at newarray {} matches the character at oldarray:{}", newarray[testnew], oldarray[oldint]);
				if (newarray[testnew] == oldarray[oldint]) {
					ret = true;
					break;
				}
			}
			if (!ret && oldarray[oldint] == FIXSTR[0]) {
				logger.debug("Recursive HITAcheck");
				ret = checkForHITA();
			}
		}
		return ret;
	}
}