Created
May 17, 2017 10:39
-
-
Save KevinTyrrell/247ce38b6a94a4a28e49f3952f78a47d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.Serializable; | |
/** | |
* Project: SecretValues | |
* Author: User | |
* Created: May 17, 2017 | |
*/ | |
public final class SecretInteger implements SecretValue<Integer>, | |
Cloneable, Comparable<SecretInteger>, Serializable | |
{ | |
/** Key used encrypt/decrypt the SecretInteger. */ | |
private static final int SECRET_KEY = 256724234; | |
private static final int MODIFIER = 12312423; | |
/** Encrypted state of the integer. */ | |
private long obfuscated; | |
public SecretInteger(final int value) | |
{ | |
set(value); | |
} | |
/** | |
* Obfuscates the data in the parameter. | |
* This function is called internally to protect | |
* the contents of the SecretValue. | |
* | |
* @param data - Data to obfuscate. | |
*/ | |
@Override | |
public void obfuscate(Integer data) | |
{ | |
obfuscated = (long)(SECRET_KEY ^ data) * SECRET_KEY + MODIFIER; | |
} | |
/** | |
* Un-does the obfuscation process. | |
* The function is called internally to reveal | |
* the protected contents of the SecretValue. | |
* | |
* @return - The un-obfuscated value of the SecretValue. | |
*/ | |
@Override | |
public Integer deObfuscate() | |
{ | |
return (int)(SECRET_KEY ^ ((obfuscated - MODIFIER) / SECRET_KEY)); | |
} | |
/** | |
* Returns the value inside this SecretValue. | |
* | |
* @return - The value of the SecretValue. | |
*/ | |
@Override | |
public Integer get() | |
{ | |
return deObfuscate(); | |
} | |
/** | |
* Sets the data inside the SecretValue to a different value. | |
* | |
* @param newValue - Value which will be set. | |
*/ | |
@Override | |
public void set(Integer newValue) | |
{ | |
obfuscate(newValue); | |
} | |
@Override | |
public int compareTo(SecretInteger o) | |
{ | |
return get().compareTo(o.get()); | |
} | |
@Override | |
protected Object clone() throws CloneNotSupportedException | |
{ | |
return new SecretInteger(get()); | |
} | |
@Override | |
public String toString() | |
{ | |
return get().toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment