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 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 Details

    • garble

      public static String garble​(String text)
      Garbles text with the default key. This can be degarbled with degarble(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

      public static String garble​(String text, String keyText)
      Garbles text with the given keyText. This can be degarbled with degarble(String, String), which must be given the same keyText.
      Parameters:
      text - the text to garble
      keyText - 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

      public static String garble​(String text, long key)
      Garbles text with the given key as a long. This can be degarbled with degarble(String, long), which must be given the same key.
      Parameters:
      text - the text to garble
      key - the key this will use to garble text
      Returns:
      a new String that appears unrelated to text and should look like gibberish
    • degarble

      public static String degarble​(String garbled)
      Given a garbled String that was produced by garble(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

      public static String degarble​(String garbled, String keyText)
      Given a garbled String that was produced by garble(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 keyText
      keyText - the keyText that was used during garbling
      Returns:
      the original String before garbling, if the keys match
    • degarble

      public static String degarble​(String garbled, long key)
      Given a garbled String that was produced by garble(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 keyText
      key - the key that was used during garbling
      Returns:
      the original String before garbling, if the keys match
    • garble

      public 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. 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 with degarble(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 garble
      keys - 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

      public static String degarble​(String garbled, long[] keys)
      Given a garbled String that was produced by garble(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 keyText
      keys - the key array that was used during garbling
      Returns:
      the original String before garbling, if the keys match
    • makeKeyArray

      public static long[] makeKeyArray​(int size, String keyText)
      If you need to produce an long array as a key for garble(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 different CrossHash.Mist objects to hash the text.
      Parameters:
      size - the size of the key array to produce; larger key arrays take proportionately longer to process
      keyText - 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[]) and degarble(String, long[])