Advertisement
Guest User

Untitled

a guest
Mar 17th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1.  
  2. /**
  3. * Simple string encoder/decoder. Also includes a simple hash calculator.
  4. *
  5. * This uses very simple algorithms, and always results in a string exactly the same size as the input string.
  6. *
  7. * @author mdixon
  8. */
  9. public class SimpleCodec {
  10.  
  11.  
  12. /**
  13. * Encodes the given string.
  14. *
  15. * @param data the string to be encoded.
  16. * @return the encoded version of the string.
  17. */
  18. String encodeString(String data) {
  19.  
  20. // get the individual bytes of the string (easier to manipulate).
  21. byte [] bytes = data.getBytes();
  22.  
  23. // create another byte array the same size (to store encoded version).
  24. byte [] outBytes = new byte[bytes.length];
  25.  
  26. // Iterate over each byte of the string
  27. for (int i=0; i < bytes.length; i++) {
  28.  
  29. outBytes[i] = (byte) (bytes[i] + 1); // very simple encoding. This would be easy to break!
  30. }
  31.  
  32. // return the encoded version converted back to a string.
  33. return new String(outBytes);
  34. }
  35.  
  36.  
  37. /**
  38. * Decodes the given string.
  39. *
  40. * @param data the string to be decoded.
  41. * @return the decoded version of the string.
  42. */
  43. String decodeString(String data) {
  44.  
  45. byte [] bytes = data.getBytes();
  46.  
  47. byte [] outBytes = new byte[bytes.length];
  48.  
  49. for (int i=0; i < bytes.length; i++) {
  50.  
  51. outBytes[i] = (byte) (bytes[i] - 1); // apply opposite calculation used for encoding, in order to decode.
  52. }
  53.  
  54. return new String(outBytes);
  55. }
  56.  
  57. /**
  58. * Generates a hash value from the given string.
  59. *
  60. * @param data the string to be hashed.
  61. * @return a hash value for the string.
  62. */
  63. String generateHash(String data) {
  64.  
  65. long hash = 0;
  66.  
  67. byte [] bytes = data.getBytes();
  68.  
  69. // Iterate over each byte of the string, and use to calculate the hash
  70. for (byte b : bytes) {
  71.  
  72. hash += b * 13;
  73. }
  74.  
  75. // create a very simple hash (total of byte values, each multiplied by a prime number, all of which is multiplied by string size, then by another prime number)
  76. hash = hash * data.length() * 997;
  77.  
  78. // make it slightly more obscure by applying a few xor operations to a shifted value.
  79. hash = hash ^ hash << 16;
  80. hash = hash ^ hash << 24;
  81.  
  82. // Format the hash to use 16 chars (since this is the max size of a long)
  83. return String.format("%016X", hash);
  84. }
  85.  
  86. /**
  87. * Simple main method that drives the example methods.
  88. *
  89. * @param args
  90. */
  91. public static void main(String[] args) {
  92.  
  93. SimpleCodec codec = new SimpleCodec();
  94.  
  95. String original = "This is a test string to be encoded";
  96.  
  97. String encoded = codec.encodeString(original);
  98. System.out.println("Encoding : " + original);
  99. System.out.println("Result : " + encoded);
  100. System.out.println();
  101.  
  102. String decoded = codec.decodeString(encoded);
  103. System.out.println("Decoding : " + encoded);
  104. System.out.println("Result : " + decoded);
  105. System.out.println();
  106.  
  107. if ( original.equals(decoded) )
  108. System.out.println("The original and decoded versions match, so the encode and decode is working");
  109. else
  110. System.out.println("The original and decoded versions do not match, so the encode and decode is failing");
  111.  
  112. System.out.println();
  113. System.out.println("Hash value of original string is : " + codec.generateHash(original));
  114. }
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement