Package squidpony
Class Garbler
java.lang.Object
squidpony.Garbler
public final class Garbler extends Object
Tools for garbling Strings (making them appear to be gibberish) and degarbling earlier outputs to get the original
inputs. This is like a weak form of encryption, and is probably enough to stop random users from editing saved files
effectively. This allows a key as a String, long, or long array to be used to affect the garbling process.
A minor step of obfuscation could be to run some combination of garble calls with different keys and then require they be degarbled by degarble calls (with the same keys as before) in the reverse order of the garble calls. This is made more efficient with the
Created by Tommy Ettinger on 5/22/2017.
A minor step of obfuscation could be to run some combination of garble calls with different keys and then require they be degarbled by degarble calls (with the same keys as before) in the reverse order of the garble calls. This is made more efficient with the
garble(String, long[])
and degarble(String, long[])
methods,
which avoid allocating multiple temporary char arrays when multiple keys are used. A more major step of obfuscation
would be to run any garbling on already-compressed text, which LZSPlus
does by using this class.
Created by Tommy Ettinger on 5/22/2017.
-
Method Summary
Modifier and Type Method Description static String
degarble(String garbled)
Given a garbled String that was produced bygarble(String)
(using the default key), this reverses the garbling and gets the original String.static String
degarble(String garbled, long key)
Given a garbled String that was produced bygarble(String, long)
(using the given key), this reverses the garbling and gets the original String.static String
degarble(String garbled, long[] keys)
Given a garbled String that was produced bygarble(String, long[])
(using the given keys), this reverses the garbling and gets the original String.static String
degarble(String garbled, String keyText)
Given a garbled String that was produced bygarble(String, String)
(using the given keyText), this reverses the garbling and gets the original String.static String
garble(String text)
Garbles text with the default key.static String
garble(String text, long key)
Garbles text with the given key as a long.static String
garble(String text, long[] keys)
Garbles text with the given keys as a long array, effectively garbling the same text one time per item in keys.static String
garble(String text, String keyText)
Garbles text with the given keyText.static long[]
makeKeyArray(int size, String keyText)
If you need to produce an long array as a key forgarble(String, long[])
when you only have a String, you can use this method if the String isn't too small (at least 8 char Strings should be fine).
-
Method Details
-
garble
Garbles text with the default key. This can be degarbled withdegarble(String)
, which also uses the default key.- Parameters:
text
- the text to garble- Returns:
- a new String that appears unrelated to text and should look like gibberish
-
garble
Garbles text with the given keyText. This can be degarbled withdegarble(String, String)
, which must be given the same keyText.- Parameters:
text
- the text to garblekeyText
- used to determine the key this will use to garble text- Returns:
- a new String that appears unrelated to text and should look like gibberish
-
garble
Garbles text with the given key as a long. This can be degarbled withdegarble(String, long)
, which must be given the same key.- Parameters:
text
- the text to garblekey
- the key this will use to garble text- Returns:
- a new String that appears unrelated to text and should look like gibberish
-
degarble
Given a garbled String that was produced bygarble(String)
(using the default key), this reverses the garbling and gets the original String.- Parameters:
garbled
- a String produced by a garble() method using the default key- Returns:
- the original String before garbling, if the keys match
-
degarble
Given a garbled String that was produced bygarble(String, String)
(using the given keyText), this reverses the garbling and gets the original String.- Parameters:
garbled
- a String produced by a garble() method using the same keyTextkeyText
- the keyText that was used during garbling- Returns:
- the original String before garbling, if the keys match
-
degarble
Given a garbled String that was produced bygarble(String, long)
(using the given key), this reverses the garbling and gets the original String.- Parameters:
garbled
- a String produced by a garble() method using the same keyTextkey
- the key that was used during garbling- Returns:
- the original String before garbling, if the keys match
-
garble
Garbles text with the given keys as a long array, effectively garbling the same text one time per item in keys. This can seen as a way to improve the quality of the shuffle by adding more bits of state to the key(s). The result can be degarbled withdegarble(String, long[])
, which must be given the same keys. This method is more efficient than calling garble() repeatedly because it only allocates one temporary char array for the whole batch of keys, as opposed to needing one temporary array per key with repeated calls.- Parameters:
text
- the text to garblekeys
- the key array this will use to garble text, as a long array- Returns:
- a new String that appears unrelated to text and should look like gibberish
-
degarble
Given a garbled String that was produced bygarble(String, long[])
(using the given keys), this reverses the garbling and gets the original String. This is not the same as calling degarble() repeatedly, in part because this uses the keys in reverse order (just like every part of the degarbling process, it needs to be in reverse), and in part because this only creates one temporary char array for the whole batch of keys, instead of creating one new char array per repeated call.- Parameters:
garbled
- a String produced by a garble() method using the same keyTextkeys
- the key array that was used during garbling- Returns:
- the original String before garbling, if the keys match
-
makeKeyArray
If you need to produce an long array as a key forgarble(String, long[])
when you only have a String, you can use this method if the String isn't too small (at least 8 char Strings should be fine). This produces a diverse array of longs without the correlation between items that you would get if you just generated a sequence of random longs from one small seed, by using multiple differentCrossHash.Mist
objects to hash the text.- Parameters:
size
- the size of the key array to produce; larger key arrays take proportionately longer to processkeyText
- the String to use as a basis for generating random-seeming numbers for keys- Returns:
- a long array that can be given to
garble(String, long[])
anddegarble(String, long[])
-