fork(4) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import static java.lang.Math.*;
  7.  
  8. /**
  9.  * String hashing functions.
  10.  * @author Arash Partow
  11.  */
  12. final class Hash
  13. {
  14.  
  15. /**
  16.   * A simple hash function from Robert Sedgwicks Algorithms in C book.
  17.   * @param str
  18.   * @return
  19.   */
  20. public static long RSHash(String str) {
  21. int b = 378551;
  22. int a = 63689;
  23. long hash = 0;
  24. for (int i = 0; i < str.length(); i++) {
  25. hash = hash * a + str.charAt(i);
  26. a = a * b;
  27. }
  28. return hash;
  29. }
  30.  
  31. /**
  32.   * A bitwise hash function written by Justin Sobel.
  33.   * @param str
  34.   * @return
  35.   */
  36. public static long JSHash(String str) {
  37. long hash = 1315423911;
  38. for (int i = 0; i < str.length(); i++) {
  39. hash ^= ((hash << 5) + str.charAt(i) + (hash >> 2));
  40. }
  41. return hash;
  42. }
  43.  
  44. /**
  45.   * This hash algorithm is based on work by Peter J. Weinberger of AT&T Bell Labs.
  46.   * @param str
  47.   * @return
  48.   */
  49. public static long PJWHash(String str) {
  50. long BitsInUnsignedInt = (long) (4 * 8);
  51. long ThreeQuarters = (long) ((BitsInUnsignedInt * 3) / 4);
  52. long OneEighth = (long) (BitsInUnsignedInt / 8);
  53. long HighBits = (long) (0xFFFFFFFF) << (BitsInUnsignedInt - OneEighth);
  54. long hash = 0;
  55. long test = 0;
  56. for (int i = 0; i < str.length(); i++) {
  57. hash = (hash << OneEighth) + str.charAt(i);
  58.  
  59. if ((test = hash & HighBits) != 0) {
  60. hash = ((hash ^ (test >> ThreeQuarters)) & (~HighBits));
  61. }
  62. }
  63. return hash;
  64. }
  65.  
  66. /**
  67.   * Similar to the PJW Hash function, but tweaked for 32-bit processors.
  68.   * Its the hash function widely used on most UNIX systems.
  69.   * @param str
  70.   * @return
  71.   */
  72. public static long ELFHash(String str) {
  73. long hash = 0;
  74. long x = 0;
  75. for (int i = 0; i < str.length(); i++) {
  76. hash = (hash << 4) + str.charAt(i);
  77. if ((x = hash & 0xF0000000L) != 0) {
  78. hash ^= (x >> 24);
  79. }
  80. hash &= ~x;
  81. }
  82. return hash;
  83. }
  84.  
  85. /**
  86.   * This hash function comes from Brian Kernighan and Dennis Ritchie's book
  87.   * "The C Programming Language". It is a simple hash function using a
  88.   * strange set of possible seeds which all constitute a pattern of
  89.   * 31....31...31 etc, it seems to be very similar to the DJB hash function.
  90.   * @param str
  91.   * @return
  92.   */
  93. public static long BKDRHash(String str) {
  94. long seed = 131; // 31 131 1313 13131 131313 etc..
  95. long hash = 0;
  96. for (int i = 0; i < str.length(); i++) {
  97. hash = (hash * seed) + str.charAt(i);
  98. }
  99. return hash;
  100. }
  101.  
  102. /**
  103.   * This is the algorithm of choice which is used in the open source
  104.   * SDBM project. The hash function seems to have a good over-all
  105.   * distribution for many different data sets. It seems to work well in
  106.   * situations where there is a high variance in the MSBs of the elements
  107.   * in a data set.
  108.   * @param str
  109.   * @return
  110.   */
  111. public static long SDBMHash(String str) {
  112. long hash = 0;
  113. for (int i = 0; i < str.length(); i++) {
  114. hash = str.charAt(i) + (hash << 6) + (hash << 16) - hash;
  115. }
  116. return hash;
  117. }
  118.  
  119. /**
  120.   * An algorithm produced by Professor Daniel J. Bernstein and shown first
  121.   * to the world on the usenet newsgroup comp.lang.c. It is one of the most
  122.   * efficient hash functions ever published.
  123.   * @param str
  124.   * @return
  125.   */
  126. public static long DJBHash(String str) {
  127. long hash = 5381;
  128. for (int i = 0; i < str.length(); i++) {
  129. hash = ((hash << 5) + hash) + str.charAt(i);
  130. }
  131. return hash;
  132. }
  133.  
  134. /**
  135.   * An algorithm proposed by Donald E. Knuth in The Art Of Computer
  136.   * Programming Volume 3, under the topic of sorting and search chapter 6.4.
  137.   * @param str
  138.   * @return
  139.   */
  140. public static long DEKHash(String str) {
  141. long hash = str.length();
  142. for (int i = 0; i < str.length(); i++) {
  143. hash = ((hash << 5) ^ (hash >> 27)) ^ str.charAt(i);
  144. }
  145. return hash;
  146. }
  147.  
  148. public static long BPHash(String str) {
  149. long hash = 0;
  150. for (int i = 0; i < str.length(); i++) {
  151. hash = hash << 7 ^ str.charAt(i);
  152. }
  153. return hash;
  154. }
  155.  
  156. public static long FNVHash(String str) {
  157. long fnv_prime = 0x811C9DC5;
  158. long hash = 0;
  159. for (int i = 0; i < str.length(); i++) {
  160. hash *= fnv_prime;
  161. hash ^= str.charAt(i);
  162. }
  163. return hash;
  164. }
  165.  
  166. /**
  167.   * An algorithm produced by me Arash Partow. I took ideas from all of the
  168.   * above hash functions making a hybrid rotative and additive hash function
  169.   * algorithm. There isn't any real mathematical analysis explaining why one
  170.   * should use this hash function instead of the others described above
  171.   * other than the fact that I tired to resemble the design as close as
  172.   * possible to a simple LFSR.
  173.   * @param str
  174.   * @return
  175.   */
  176. public static long APHash(String str) {
  177. long hash = 0xAAAAAAAA;
  178. for (int i = 0; i < str.length(); i++) {
  179. if ((i & 1) == 0) {
  180. hash ^= ((hash << 7) ^ str.charAt(i) * (hash >> 3));
  181. } else {
  182. hash ^= (~((hash << 11) + str.charAt(i) ^ (hash >> 5)));
  183. }
  184. }
  185. return hash;
  186. }
  187.  
  188. }
  189. /**
  190.  * Password generator class.
  191.  * @author megavillain@gmail.com
  192.  */
  193. final class PasswordGenerator {
  194.  
  195. public PasswordGenerator() {
  196. super();
  197. reset();
  198. }
  199.  
  200. /**
  201.   * Clear all fields.
  202.   */
  203. public void reset() {
  204. skeletonKey = null;
  205. userName = null;
  206. host = null;
  207. password = null;
  208. }
  209.  
  210. /**
  211.   * Generates password.
  212.   * @throws Exception
  213.   */
  214. public void generate() throws IllegalArgumentException {
  215. if ((skeletonKey == null) || (host == null) || (userName == null) ||
  216. ("".equals(skeletonKey)) || ("".equals(host)) || ("".equals(userName)) ) {
  217. throw new IllegalArgumentException("Insufficient data.");
  218. }
  219. final long EVEN_MAGIC = 0x78D0A543;
  220. final long ODD_MAGIC = 0xF8295CE1;
  221. long evenHash = Hash.PJWHash(userName) ^ Hash.DJBHash(skeletonKey) ^ Hash.SDBMHash(host) ^ EVEN_MAGIC;
  222. long oddHash = Hash.SDBMHash(new StringBuffer(userName).reverse().toString()) ^
  223. Hash.PJWHash(new StringBuffer(skeletonKey).reverse().toString()) ^
  224. Hash.DJBHash(new StringBuffer(host).reverse().toString()) ^ ODD_MAGIC;
  225. final int PASSWORD_LENGTH = (int) (BASE_PASSWORD_LENGTH + abs(((Hash.APHash(host) ^ Hash.FNVHash(userName)) % BASE_PASSWORD_LENGTH)));
  226. StringBuilder builder = new StringBuilder(PASSWORD_LENGTH);
  227. for (int i = 0; i < PASSWORD_LENGTH; i++) {
  228. if ((i % 2) == 0) {
  229. builder.append(PASSWORD_CHARS.charAt((int) abs(((evenHash ^ (i * EVEN_MAGIC)) % PASSWORD_CHARS.length()))));
  230. } else {
  231. builder.append(PASSWORD_CHARS_REVERSED.charAt((int) abs(((oddHash ^ (i * ODD_MAGIC)) % PASSWORD_CHARS_REVERSED.length()))));
  232. }
  233. }
  234. password = builder.toString();
  235. }
  236.  
  237. public void setSkeletonKey(final String value) {
  238. skeletonKey = value;
  239. }
  240.  
  241. public String getSkeletonKey() {
  242. return skeletonKey;
  243. }
  244.  
  245. public void setUserName(final String value) {
  246. userName = value;
  247. }
  248.  
  249. public String getUserName() {
  250. return userName;
  251. }
  252.  
  253. public void setHost(final String value) {
  254. host = value;
  255. }
  256.  
  257. public String getHost() {
  258. return host;
  259. }
  260.  
  261. public String getPassword() { //TODO: fix it
  262. return password;
  263. }
  264.  
  265. private String skeletonKey = null; // user's passphrase
  266. private String userName = null; // user's name
  267. private String host = null; // password for what
  268. private String password = null; // password itself
  269.  
  270. private static final int BASE_PASSWORD_LENGTH = 8;
  271. private static final String PASSWORD_CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  272. private static final String PASSWORD_CHARS_REVERSED = new StringBuffer(PASSWORD_CHARS).reverse().toString();
  273. }
  274.  
  275.  
  276. /* Name of the class has to be "Main" only if the class is public. */
  277. class Ideone
  278. {
  279. public static void main (String[] args) throws java.lang.Exception
  280. {
  281. PasswordGenerator a = new PasswordGenerator();
  282. a.setUserName("random");
  283. a.setSkeletonKey("random");
  284. a.setHost("random");
  285. a.generate();
  286. System.out.println(a.getPassword());
  287. }
  288. }
Success #stdin #stdout 0.1s 320256KB
stdin
Standard input is empty
stdout
F6VSBsL8H8zo