UglyFix.java 1.88 KB
package fi.insomnia.bortal.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;
	}
}