Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
2.0 KiB

  1. /*++
  2. Copyright (C) 1999-2001 Microsoft Corporation
  3. Module Name:
  4. Abstract:
  5. History:
  6. --*/
  7. #include <windows.h>
  8. #include <stdio.h>
  9. typedef unsigned __int64 LUINT;
  10. #define LARGE_PRIME 2147483659
  11. DWORD HashKey(char* szKey);
  12. LUINT Invert(LUINT n, LUINT p = LARGE_PRIME);
  13. void main(int argc, char** argv)
  14. {
  15. if(argc < 3)
  16. {
  17. printf("Usage: egg <first> <second>\n");
  18. return;
  19. }
  20. FILE* f1 = fopen(argv[1], "r");
  21. if(f1 == NULL)
  22. {
  23. printf("Cannot open %s\n", argv[1]);
  24. return;
  25. }
  26. FILE* f2 = fopen(argv[2], "r");
  27. if(f2 == NULL)
  28. {
  29. printf("Cannot open %s\n", argv[2]);
  30. return;
  31. }
  32. char szKey1[100];
  33. char szKey2[100];
  34. fgets(szKey1, 100, f1);
  35. szKey1[strlen(szKey1)-1] = 0;
  36. fgets(szKey2, 100, f2);
  37. szKey2[strlen(szKey2)-1] = 0;
  38. char* szText1 = new char[100000];
  39. memset(szText1, 0, 100000);
  40. char* szText2 = new char[100000];
  41. memset(szText2, 0, 100000);
  42. int i;
  43. i = 0;
  44. while((szText1[i] = getc(f1)) != EOF) i++;
  45. szText1[i] = 0;
  46. i = 0;
  47. while((szText2[i] = getc(f2)) != EOF) i++;
  48. szText2[i] = 0;
  49. LUINT nK1 = HashKey(szKey1);
  50. LUINT nK2 = HashKey(szKey2);
  51. LUINT nDiff = (nK1 > nK2)?nK1-nK2:LARGE_PRIME-(nK2-nK1);
  52. LUINT nDet = Invert(nDiff);
  53. LUINT nA, nB, nC, nD;
  54. nA = nDet;
  55. nB = LARGE_PRIME - nDet;
  56. nC = (nB * nK2) % LARGE_PRIME;
  57. nD = (nA * nK1) % LARGE_PRIME;
  58. int nTextLen = (strlen(szText1)+1) / sizeof(DWORD) + 1;
  59. DWORD* pnText1 = (DWORD*)szText1;
  60. DWORD* pnText2 = (DWORD*)szText2;
  61. for(i = 0; i < nTextLen; i++)
  62. {
  63. __int64 z1 = pnText1[i];
  64. __int64 z2 = pnText2[i];
  65. __int64 s1 = (z1*nA + z2*nB) % LARGE_PRIME;
  66. __int64 s2 = (z1*nC + z2*nD) % LARGE_PRIME;
  67. __int64 zz2 = (s1*nK2 + s2) % LARGE_PRIME;
  68. printf("\t%lu, %lu, \n", (DWORD)s1, (DWORD)s2);
  69. }
  70. }
  71. DWORD HashKey(char* szKey)
  72. {
  73. DWORD n = 1;
  74. while(*szKey)
  75. {
  76. n = n * (2*(DWORD)*szKey + 1);
  77. szKey++;
  78. }
  79. return n % LARGE_PRIME;
  80. }
  81. LUINT Invert(LUINT n, LUINT p)
  82. {
  83. LUINT nPower = p-2;
  84. __int64 nResult = 1;
  85. for(int i = 31; i >= 0; i--)
  86. {
  87. nResult = (nResult*nResult) % p;
  88. if(nPower & (1 << i))
  89. {
  90. nResult = (nResult*n) % p;
  91. }
  92. }
  93. return (LUINT)nResult;
  94. }