Ejemplo n.º 1
0
        /// <summary>
        /// Generate hash for passwords
        /// </summary>
        /// <param name="indata"></param>
        /// <param name="challenge"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static string GenerateHash(string indata, string challenge, Options options)
        {
            Sum sum = new Sum(options);

            sum.Init(0);
            sum.Update(Encoding.ASCII.GetBytes(indata), 0, indata.Length);
            sum.Update(Encoding.ASCII.GetBytes(challenge), 0, challenge.Length);
            byte[] buf = sum.End();
            string hash = Convert.ToBase64String(buf);
            return hash.Substring(0, (buf.Length * 8 + 5) / 6);
        }
Ejemplo n.º 2
0
		public static string gen_challenge(string addr, Options opt)
		{			
			string challenge = "";
			byte[] input = new byte[32];			
			DateTime tv = DateTime.Now;			
			
			for(int i=0; i < addr.Length; i++)
				input[i] = Convert.ToByte(addr[i]);
						
			CheckSum.SIVAL(ref input, 16, (UInt32)tv.Second);
			CheckSum.SIVAL(ref input, 20, (UInt32)tv.Hour);
			CheckSum.SIVAL(ref input, 24, (UInt32)tv.Day);

			Sum sum = new Sum(opt);
			sum.Init(0);
			sum.Update(input,0,input.Length);
			challenge = Encoding.ASCII.GetString(sum.End());
			return challenge;
		}
Ejemplo n.º 3
0
		public void HashSearch(IOStream f,SumStruct s, MapFile buf, int len, Sum _sum)
		{
			int offset, end, backup;
			UInt32 k;
			int wantI;
			byte[] sum2 = new byte[CheckSum.SUM_LENGTH];
			UInt32 s1, s2, sum;
			int more;
			byte[] map;
			
			wantI = 0;
			if (options.verbose > 2)
				Log.WriteLine("hash search ob=" + s.bLength +" len=" + len);

			k = (UInt32)Math.Min(len, s.bLength);
			int off = buf.MapPtr(0, (int)k);
			map = buf.p;
			
			UInt32 g = s.sums[0].sum1;
			sum = CheckSum.GetChecksum1(map, off, (int)k);
			s1 = sum & 0xFFFF;
			s2 = sum >> 16;
			if (options.verbose > 3)
				Log.WriteLine("sum=" + sum +" k=" + k);

			offset = 0;
			end = (int)(len + 1 - s.sums[s.count-1].len);
			if (options.verbose > 3)
				Log.WriteLine("hash search s.bLength=" + s.bLength +" len=" + len +" count=" + s.count);

			do 
			{
				UInt32 t = GetTag2(s1,s2);
				bool doneCsum2 = false;
				int j = tagTable[t];

				if (options.verbose > 4)
					Log.WriteLine("offset=" + offset + " sum=" + sum);

				if (j == NULL_TAG)
					goto null_tag;

				sum = (s1 & 0xffff) | (s2 << 16);
				tagHits++;
				do 
				{
					UInt32 l;
					int i = ((Target)targets[j]).i;

					if (sum != s.sums[i].sum1)
						continue;

					l = (UInt32)Math.Min(s.bLength, len-offset);
					if (l != s.sums[i].len)
						continue;

					if (options.verbose > 3)
						Log.WriteLine("potential match at " + offset + " target=" + j +" " + i + " sum=" + sum);

					if (!doneCsum2) 
					{
						off = buf.MapPtr(offset, (int)l);
						map = buf.p;
						CheckSum cs = new CheckSum(options); 
						sum2 = cs.GetChecksum2(map, off, (int)l);
						doneCsum2 = true;
					}

					if (Util.MemCmp(sum2, 0, s.sums[i].sum2, 0, s.s2Length) != 0) 
					{
						falseAlarms++;
						continue;
					}
					
					if (i != wantI && wantI < s.count
						&& (!options.inplace || options.makeBackups || s.sums[wantI].offset >= offset
						|| (s.sums[wantI].flags & SUMFLG_SAME_OFFSET) != 0)
						&& sum == s.sums[wantI].sum1
						&& Util.MemCmp(sum2, 0, s.sums[wantI].sum2, 0, s.s2Length) == 0) 
					{
						i = wantI;
					}
				set_want_i:
					wantI = i + 1;

					Matched(f,s,buf,offset,i,_sum);
					offset += (int)(s.sums[i].len - 1);
					k = (UInt32)Math.Min(s.bLength, len-offset);
					off = buf.MapPtr(offset, (int)k);					
					sum = CheckSum.GetChecksum1(map, off, (int)k);
					s1 = sum & 0xFFFF;
					s2 = sum >> 16;
					matches++;
					break;
				} while (++j < s.count && ((Target)targets[j]).t == t);								
			null_tag:
				backup = offset - lastMatch;
				if (backup < 0)
					backup = 0;

				more = (offset + k) < len ? 1 : 0;
				off = buf.MapPtr(offset - backup, (int)(k + more + backup))+ backup;				
				s1 -= (UInt32)(CheckSum.ToInt(map[off]) + CheckSum.CHAR_OFFSET);
				s2 -= (UInt32)(k * CheckSum.ToInt(map[off]) + CheckSum.CHAR_OFFSET);				
				off = (k + off >= map.Length) ? (int)(map.Length-k-1) : off;
				if (more != 0) 
				{
					s1 += (UInt32)(CheckSum.ToInt(map[k + off]) + CheckSum.CHAR_OFFSET);
					s2 += s1;
				} 
				else
					--k;

				if (backup >= CHUNK_SIZE + s.bLength && end - offset > CHUNK_SIZE)
					Matched(f,s,buf,(int)(offset - s.bLength), -2, _sum);
			} while (++offset < end);

			Matched(f,s,buf,len,-1, _sum);
			buf.MapPtr(len-1,1);
		}
Ejemplo n.º 4
0
		public void Matched(IOStream f, SumStruct s, MapFile buf, int offset, int i, Sum sum)
		{
			int n = offset - lastMatch;
			int j;

			if (options.verbose > 2 && i >= 0)
				Log.WriteLine("match at " + offset +" last_match=" + lastMatch + " j=" + i + " len=" + s.sums[i].len + " n=" + n);

			Token token = new Token(options);
			token.SendToken(f,i,buf,lastMatch,n,(int)(i<0?0:s.sums[i].len));
			dataTransfer += n;

			if (i >= 0) 
			{
				Options.stats.matchedData += s.sums[i].len;
				n += (int)s.sums[i].len;
			}

			for (j = 0; j < n; j += CHUNK_SIZE) 
			{
				int n1 = Math.Min(CHUNK_SIZE,n-j);
				int off = buf.MapPtr(lastMatch + j, n1);
				sum.Update(buf.p , off, n1);
			}

			if (i >= 0)
				lastMatch = (int)(offset + s.sums[i].len);
			else
				lastMatch = offset;

			if (buf != null && options.doProgress) 
			{
				Progress.ShowProgress(lastMatch, buf.fileSize);
				if (i == -1)
					Progress.EndProgress(buf.fileSize);
			}
		}
Ejemplo n.º 5
0
		public void MatchSums(IOStream f, SumStruct s, MapFile buf, int len)
		{
			byte[] fileSum = new byte[CheckSum.MD4_SUM_LENGTH];

			lastMatch = 0;
			falseAlarms = 0;
			tagHits = 0;
			matches = 0;
			dataTransfer = 0;

			Sum sum = new Sum(options);
			sum.Init(options.checksumSeed);

			if (len > 0 && s.count>0) 
			{
				BuildHashTable(s);

				if (options.verbose > 2)
					Log.WriteLine("built hash table");

				HashSearch(f,s,buf,len, sum);

				if (options.verbose > 2)
					Log.WriteLine("done hash search");
			} 
			else 
			{
				for (int j = 0; j < len - CHUNK_SIZE; j += CHUNK_SIZE) 
				{
					int n1 = Math.Min(CHUNK_SIZE,(len-CHUNK_SIZE)-j);
					Matched(f,s,buf,j+n1,-2, sum);
				}
				Matched(f,s,buf,len,-1,sum);
			}

			fileSum = sum.End();
			if (buf != null && buf.status)
				fileSum[0]++;

			if (options.verbose > 2)
				Log.WriteLine("sending fileSum");
			f.Write(fileSum, 0, CheckSum.MD4_SUM_LENGTH);

			targets.Clear();

			if (options.verbose > 2)
				Log.WriteLine("falseAlarms=" +  falseAlarms + " tagHits=" + tagHits + " matches=" + matches);

			totalTagHits += tagHits;
			totalFalseAlarms += falseAlarms;
			totalMatches += matches;
			Options.stats.literalData += dataTransfer;
		}
Ejemplo n.º 6
0
        public bool ReceiveData(ClientInfo clientInfo, string fileNameR, Stream fdR, long sizeR, string fileName, Stream fd, int totalSize)
        {
            IOStream f = clientInfo.IoStream;
            byte[] fileSum1 = new byte[CheckSum.MD4_SUM_LENGTH];
            byte[] fileSum2 = new byte[CheckSum.MD4_SUM_LENGTH];
            byte[] data = new byte[Match.CHUNK_SIZE];
            SumStruct sumStruct = new SumStruct();
            MapFile mapBuf = null;
            Sender sender = new Sender(options);
            sender.ReadSumHead(clientInfo, ref sumStruct);
            int offset = 0;
            UInt32 len;

            if (fdR != null && sizeR > 0)
            {
                int mapSize = (int)Math.Max(sumStruct.bLength * 2, 16 * 1024);
                mapBuf = new MapFile(fdR, (int)sizeR, mapSize, (int)sumStruct.bLength);
                if (options.verbose > 2)
                {
                    Log.WriteLine("recv mapped " + fileNameR + " of size " + sizeR);
                }
            }
            Sum sum = new Sum(options);
            sum.Init(options.checksumSeed);

            int i;
            Token token = new Token(options);
            while ((i = token.ReceiveToken(f, ref data, 0)) != 0)
            {
                if (options.doProgress)
                {
                    Progress.ShowProgress(offset, totalSize);
                }

                if (i > 0)
                {
                    if (options.verbose > 3)
                    {
                        Log.WriteLine("data recv " + i + " at " + offset);
                    }
                    Options.stats.literalData += i;
                    sum.Update(data, 0, i);
                    if (fd != null && FileIO.WriteFile(fd, data, 0, i) != i)
                    {
                        goto report_write_error;
                    }
                    offset += i;
                    continue;
                }

                i = -(i + 1);
                int offset2 = (int)(i * sumStruct.bLength);
                len = sumStruct.bLength;
                if (i == sumStruct.count - 1 && sumStruct.remainder != 0)
                {
                    len = sumStruct.remainder;
                }

                Options.stats.matchedData += len;

                if (options.verbose > 3)
                {
                    Log.WriteLine("chunk[" + i + "] of size " + len + " at " + offset2 + " offset=" + offset);
                }

                byte[] map = null;
                int off = 0;
                if (mapBuf != null)
                {
                    off = mapBuf.MapPtr(offset2, (int)len);
                    map = mapBuf.p;

                    token.SeeToken(map, offset, (int)len);
                    sum.Update(map, off, (int)len);
                }

                if (options.inplace)
                {
                    if (offset == offset2 && fd != null)
                    {
                        offset += (int)len;
                        if (fd.Seek(len, SeekOrigin.Current) != offset)
                        {
                            MainClass.Exit("seek failed on " + Util.fullFileName(fileName), clientInfo);
                        }
                        continue;
                    }
                }
                if (fd != null && FileIO.WriteFile(fd, map, off, (int)len) != (int)len)
                {
                    goto report_write_error;
                }
                offset += (int)len;
            }

            if (options.doProgress)
            {
                Progress.EndProgress(totalSize);
            }
            if (fd != null && offset > 0 && FileIO.SparseEnd(fd) != 0)
            {
                MainClass.Exit("write failed on " + Util.fullFileName(fileName), clientInfo);
            }

            fileSum1 = sum.End();

            if (mapBuf != null)
            {
                mapBuf = null;
            }

            fileSum2 = f.ReadBuffer(CheckSum.MD4_SUM_LENGTH);
            if (options.verbose > 2)
            {
                Log.WriteLine("got fileSum");
            }
            if (fd != null && Util.MemoryCompare(fileSum1, 0, fileSum2, 0, CheckSum.MD4_SUM_LENGTH) != 0)
            {
                return false;
            }
            return true;
        report_write_error:
            {
                MainClass.Exit("write failed on " + Util.fullFileName(fileName), clientInfo);
            }
            return true;
        }
Ejemplo n.º 7
0
        public bool ReceiveData(ClientInfo clientInfo, string fileNameR, Stream fdR, long sizeR, string fileName, Stream fd, int totalSize)
        {
            IOStream f = clientInfo.IoStream;

            byte[]    fileSum1  = new byte[CheckSum.MD4_SUM_LENGTH];
            byte[]    fileSum2  = new byte[CheckSum.MD4_SUM_LENGTH];
            byte[]    data      = new byte[Match.CHUNK_SIZE];
            SumStruct sumStruct = new SumStruct();
            MapFile   mapBuf    = null;
            Sender    sender    = new Sender(options);

            sender.ReadSumHead(clientInfo, ref sumStruct);
            int    offset = 0;
            UInt32 len;

            if (fdR != null && sizeR > 0)
            {
                int mapSize = (int)Math.Max(sumStruct.bLength * 2, 16 * 1024);
                mapBuf = new MapFile(fdR, (int)sizeR, mapSize, (int)sumStruct.bLength);
                if (options.verbose > 2)
                {
                    Log.WriteLine("recv mapped " + fileNameR + " of size " + sizeR);
                }
            }
            Sum sum = new Sum(options);

            sum.Init(options.checksumSeed);

            int   i;
            Token token = new Token(options);

            while ((i = token.ReceiveToken(f, ref data, 0)) != 0)
            {
                if (options.doProgress)
                {
                    Progress.ShowProgress(offset, totalSize);
                }

                if (i > 0)
                {
                    if (options.verbose > 3)
                    {
                        Log.WriteLine("data recv " + i + " at " + offset);
                    }
                    Options.stats.literalData += i;
                    sum.Update(data, 0, i);
                    if (fd != null && FileIO.WriteFile(fd, data, 0, i) != i)
                    {
                        goto report_write_error;
                    }
                    offset += i;
                    continue;
                }

                i = -(i + 1);
                int offset2 = (int)(i * sumStruct.bLength);
                len = sumStruct.bLength;
                if (i == sumStruct.count - 1 && sumStruct.remainder != 0)
                {
                    len = sumStruct.remainder;
                }

                Options.stats.matchedData += len;

                if (options.verbose > 3)
                {
                    Log.WriteLine("chunk[" + i + "] of size " + len + " at " + offset2 + " offset=" + offset);
                }

                byte[] map = null;
                int    off = 0;
                if (mapBuf != null)
                {
                    off = mapBuf.MapPtr(offset2, (int)len);
                    map = mapBuf.p;

                    token.SeeToken(map, offset, (int)len);
                    sum.Update(map, off, (int)len);
                }

                if (options.inplace)
                {
                    if (offset == offset2 && fd != null)
                    {
                        offset += (int)len;
                        if (fd.Seek(len, SeekOrigin.Current) != offset)
                        {
                            MainClass.Exit("seek failed on " + Util.fullFileName(fileName), clientInfo);
                        }
                        continue;
                    }
                }
                if (fd != null && FileIO.WriteFile(fd, map, off, (int)len) != (int)len)
                {
                    goto report_write_error;
                }
                offset += (int)len;
            }

            if (options.doProgress)
            {
                Progress.EndProgress(totalSize);
            }
            if (fd != null && offset > 0 && FileIO.SparseEnd(fd) != 0)
            {
                MainClass.Exit("write failed on " + Util.fullFileName(fileName), clientInfo);
            }

            fileSum1 = sum.End();

            if (mapBuf != null)
            {
                mapBuf = null;
            }

            fileSum2 = f.ReadBuffer(CheckSum.MD4_SUM_LENGTH);
            if (options.verbose > 2)
            {
                Log.WriteLine("got fileSum");
            }
            if (fd != null && Util.MemoryCompare(fileSum1, 0, fileSum2, 0, CheckSum.MD4_SUM_LENGTH) != 0)
            {
                return(false);
            }
            return(true);

report_write_error:
            {
                MainClass.Exit("write failed on " + Util.fullFileName(fileName), clientInfo);
            }
            return(true);
        }