Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1.  
  2. import Base32;
  3.  
  4. import java.security.MessageDigest;
  5.  
  6. public class Sha1HashExtension implements AdcHashExtension {
  7.  
  8. public String getExtensionName() {
  9. return "SHA1";
  10. }
  11.  
  12. public byte[] hash(byte[] bytes) {
  13. byte[] result = null;
  14. try {
  15. MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
  16. messageDigest.update(bytes);
  17. result = messageDigest.digest();
  18. } catch (Exception exception) {
  19. exception.printStackTrace();
  20. }
  21. return result;
  22. }
  23.  
  24. public String hash(String baseEncodedData) {
  25. return Base32.encode(hash(Base32.decode(baseEncodedData)));
  26. }
  27.  
  28. public String password(String baseEncodedCid, String password, String baseEncodedKey) {
  29. String result = null;
  30. try {
  31. MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
  32. messageDigest.update(Base32.decode(baseEncodedCid));
  33. messageDigest.update(password.getBytes());
  34. messageDigest.update(Base32.decode(baseEncodedKey));
  35. result = Base32.encode(messageDigest.digest());
  36. } catch (Exception exception) {
  37. exception.printStackTrace();
  38. }
  39. return result;
  40. }
  41. }
  42.  
  43. public class Base32 {
  44. private static final String base32Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
  45. private static final int[] base32Lookup = { 0xFF, 0xFF, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, // '0',
  46. // '1',
  47. // '2',
  48. // '3',
  49. // '4', '5', '6', '7'
  50. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // '8', '9', ':',
  51. // ';', '<', '=',
  52. // '>', '?'
  53. 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // '@', 'A', 'B',
  54. // 'C', 'D', 'E',
  55. // 'F', 'G'
  56. 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, // 'H', 'I', 'J',
  57. // 'K', 'L', 'M',
  58. // 'N', 'O'
  59. 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, // 'P', 'Q', 'R',
  60. // 'S', 'T', 'U',
  61. // 'V', 'W'
  62. 0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 'X', 'Y', 'Z',
  63. // '[', '\', ']',
  64. // '^', '_'
  65. 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // '`', 'a', 'b',
  66. // 'c', 'd', 'e',
  67. // 'f', 'g'
  68. 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, // 'h', 'i', 'j',
  69. // 'k', 'l', 'm',
  70. // 'n', 'o'
  71. 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, // 'p', 'q', 'r',
  72. // 's', 't', 'u',
  73. // 'v', 'w'
  74. 0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF // 'x', 'y', 'z',
  75. // '{', '|', '}',
  76. // '~', 'DEL'
  77. };
  78.  
  79. static public byte[] decode(final String base32) {
  80. int i, index, lookup, offset, digit;
  81. byte[] bytes = new byte[base32.length() * 5 / 8];
  82.  
  83. for (i = 0, index = 0, offset = 0; i < base32.length(); i++) {
  84. lookup = base32.charAt(i) - '0';
  85.  
  86. /* Skip chars outside the lookup table */
  87. if (lookup < 0 || lookup >= base32Lookup.length) {
  88. continue;
  89. }
  90.  
  91. digit = base32Lookup[lookup];
  92.  
  93. /* If this digit is not in the table, ignore it */
  94. if (digit == 0xFF) {
  95. continue;
  96. }
  97.  
  98. if (index <= 3) {
  99. index = (index + 5) % 8;
  100. if (index == 0) {
  101. bytes[offset] |= digit;
  102. offset++;
  103. if (offset >= bytes.length) {
  104. break;
  105. }
  106. } else {
  107. bytes[offset] |= digit << 8 - index;
  108. }
  109. } else {
  110. index = (index + 5) % 8;
  111. bytes[offset] |= digit >>> index;
  112. offset++;
  113.  
  114. if (offset >= bytes.length) {
  115. break;
  116. }
  117. bytes[offset] |= digit << 8 - index;
  118. }
  119. }
  120. return bytes;
  121. }
  122.  
  123. static public String encode(final byte[] bytes) {
  124. int i = 0, index = 0, digit = 0;
  125. int currByte, nextByte;
  126. StringBuffer base32 = new StringBuffer((bytes.length + 7) * 8 / 5);
  127.  
  128. while (i < bytes.length) {
  129. currByte = bytes[i] >= 0 ? bytes[i] : bytes[i] + 256; // unsign
  130.  
  131. /* Is the current digit going to span a byte boundary? */
  132. if (index > 3) {
  133. if (i + 1 < bytes.length) {
  134. nextByte = bytes[i + 1] >= 0 ? bytes[i + 1] : bytes[i + 1] + 256;
  135. } else {
  136. nextByte = 0;
  137. }
  138.  
  139. digit = currByte & 0xFF >> index;
  140. index = (index + 5) % 8;
  141. digit <<= index;
  142. digit |= nextByte >> 8 - index;
  143. i++;
  144. } else {
  145. digit = currByte >> 8 - (index + 5) & 0x1F;
  146. index = (index + 5) % 8;
  147. if (index == 0) {
  148. i++;
  149. }
  150. }
  151. base32.append(base32Chars.charAt(digit));
  152. }
  153.  
  154. return base32.toString();
  155. }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement