Guest User

Untitled

a guest
Feb 18th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.List;
  4.  
  5. public class TKCalculateUtils {
  6.  
  7. public static long hexCharAsNumber(char c) {
  8. if (c >= 'a') {
  9. return c - 87;
  10. } else {
  11. return Long.valueOf(String.valueOf(c));
  12. }
  13. }
  14.  
  15. public static long normalizeHash(long encondindRound2) {
  16. if (encondindRound2 < 0) {
  17. encondindRound2 = (encondindRound2 & 0x7fffffffL) + 0x80000000L;
  18. }
  19. return encondindRound2 % 1000000L;
  20. }
  21.  
  22. public static List<Long> transformQuery(String query) {
  23. List<Long> result = new ArrayList<>();
  24.  
  25. for (int i = 0; i < query.length(); i++) {
  26. char c = query.charAt(i);
  27. if (c < 128) {
  28. result.add((long) c); // 0{l[6-0]}
  29. } else if (c < 2048) {
  30. result.add(c >> 6 | 0xC0L); // 110{l[10-6]}
  31. result.add(c & 0x3F | 0x80L); // 10{l[5-0]}
  32. } else if (0xD800L == (c & 0xFC00L) && c + 1 < query.length() && 0xDC00L == (query.charAt(i + 1) & 0xFC00L)) {
  33. // that's pretty rare... (avoid ovf?)
  34. c = (char) ((1 << 16) + ((c & 0x03FF) << 10) + (query.charAt(++i) & 0x03FF));
  35. result.add(c >> 18 | 0xF0L); // 111100{l[9-8*]}
  36. result.add(c >> 12 & 0x3FL | 0x80L); // 10{l[7*-2]}
  37. result.add(c & 0x3FL | 0x80L); // 10{(l+1)[5-0]}
  38. } else {
  39. result.add(c >> 12 | 0xE0L); // 1110{l[15-12]}
  40. result.add(c >> 6 & 0x3FL | 0x80L); // 10{l[11-6]}
  41. result.add(c & 0x3FL | 0x80L); // 10{l[5-0]}
  42. }
  43. }
  44. return result;
  45. }
  46.  
  47. public static long shiftLeftOrRightThenSumOrXor(List<String> opArray, long num) {
  48. for (String opString : opArray) {
  49. char op1 = opString.charAt(1); // '+' | '-' ~ SUM | XOR
  50. char op2 = opString.charAt(0); // '+' | '^' ~ SLL | SRL
  51. char xd = opString.charAt(2); // [0-9a-f]
  52.  
  53. long shiftAmount = hexCharAsNumber(xd);
  54. long mask = (op1 == '+') ? num >>> shiftAmount : num << shiftAmount;
  55. num = (op2 == '+') ? (num + mask & 0xffffffffL) : (num ^ mask);
  56. }
  57.  
  58. return num;
  59. }
  60.  
  61. /*
  62. / EXAMPLE:
  63. /
  64. / INPUT: query: 'hola', windowTkk: '409837.2120040981'
  65. / OUTPUT: '70528.480109'
  66. /
  67. */
  68. public static String calcHash(String query, String windowTkk) {
  69. // STEP 1: spread the the query char codes on a byte-array, 1-3 bytes per char
  70. List<Long> bytesArray = transformQuery(query);
  71.  
  72. // STEP 2: starting with TKK index, add the array from last step one-by-one, and do 2 rounds of shift+add/xor
  73. String[] d = windowTkk.split("\\.");
  74. long tkkIndex = Long.valueOf(d[0]);
  75. long tkkKey = Long.valueOf(d[1]);
  76.  
  77. long acc = tkkIndex;
  78. for (Long current : bytesArray) {
  79. acc += current;
  80. acc = shiftLeftOrRightThenSumOrXor(Arrays.asList("+-a", "^+6"), acc);
  81. }
  82.  
  83. long encondingRound1 = acc;
  84.  
  85.  
  86. // STEP 3: apply 3 rounds of shift+add/xor and XOR with they TKK key
  87. long encondingRound2 = shiftLeftOrRightThenSumOrXor(Arrays.asList("+-3", "^+b", "+-f"), encondingRound1) ^ tkkKey;
  88.  
  89. // STEP 4: Normalize to 2s complement & format
  90. long normalizedResult = normalizeHash(encondingRound2);
  91.  
  92. return normalizedResult + "." + (normalizedResult ^ tkkIndex);
  93. }
  94.  
  95. }
Add Comment
Please, Sign In to add comment