package org.openslx.dozmod.authentication; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; import org.apache.commons.codec.binary.Base64; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.openslx.dozmod.Config; public class FingerprintManager { private static final Logger LOGGER = LogManager.getLogger(FingerprintManager.class); private static final File file = new File(Config.getPath(), "fingerprints.properties"); private static final Properties prop = new Properties(); static { if (file.exists()) { try { prop.load(new FileInputStream(file)); } catch (IOException e) { LOGGER.warn("Could not load cached fingerprints from " + file.toString()); } } } public static void saveKnownFingerprint(String address, byte[] fingerprint) { if (saveFingerprint(address, fingerprint, true)) { store(); } } public static void saveSuggestedFingerprint(String address, byte[] fingerprint) { saveFingerprint(address, fingerprint, false); prop.setProperty(address + "_master", Base64.encodeBase64String(fingerprint)); store(); } private static boolean saveFingerprint(String address, byte[] fingerprint, boolean replace) { if (replace || !prop.containsKey(address)) { prop.setProperty(address, Base64.encodeBase64String(fingerprint)); return true; } return false; } private static void store() { try { prop.store(new FileOutputStream(file), "Written by bwLehrstuhl"); } catch (IOException e) { LOGGER.warn("Could not store fingerprint"); } } /** * Get the fingerprint we accepted previously for this address. * * @param address * @return fingerprint, null if unknown */ public static byte[] getKnownFingerprint(String address) { return Base64.decodeBase64(prop.getProperty(address)); } /** * Get the fingerprint the master server suggests should be * the one for the given address. * * @param address * @return fingerprint, null if unknown */ public static byte[] getSuggestedFingerprint(String address) { return Base64.decodeBase64(prop.getProperty(address + "_master")); } }