All pastes #954189 Raw Edit

psicho

public text v1 · immutable
#954189 ·published 2008-03-23 19:06 UTC
rendered paste body
    private void CreateChain(ref List<Piece> chain,byte[] startword)
        {
            if (chain.Count > 0)
                return;         //vyzadujeme prazdy retazec na zaciatku!

            Piece piece = new Piece();

            //najpr vytvoryme zaciatok retazca [0-krok]
            piece.word = startword;
            piece.hash = settings.hashAlgo.ComputeHash(piece.word);

            chain.Add(piece); // pridame prvy na zaciatku

            for (int poz = 1; poz < settings.chainLen - 1; poz++) //v kazdej iteracii rozvijame retazec o jeden prvok
            {
                //najprv ziskame nove slovo z predchadzajucej hash hodnoty
                //pomocou redukcnej funkcii ktora prislucha danemu
                // "stplcu" v retazci
                //
                // prvok01 -> RedukcnaFunkcia01 -> prvok02 -> RedukcnaFunkcia02 -> .... -> RedukcnaFunkciaN -> prvokN
                //

                piece = new Piece();
                piece.word = ReductionFunction(chain[poz - 1].hash, poz);  //redukujeme hash na slovo pomocou prislusnej funkcie
                piece.hash = settings.hashAlgo.ComputeHash(piece.word);
                chain.Add(piece);             
            }

            //ukoncime retazec ,pridanim posledneho prvku ktory obsahuje uz len hash
            piece = new Piece();
            piece.word = null;
            piece.hash = settings.hashAlgo.ComputeHash(chain[settings.chainLen - 2].word);

        }

        private byte[] ReductionFunction(byte[] hash,int poz)
        {
            ASCIIEncoding asci = new ASCIIEncoding();
            byte[] word = new byte[settings.wordLen];
            
            
            for(int i=0; i < hash.Length; i++)
            {
                hash[i] =(byte)( hash[i] ^ poz);
               
            }

            char[] chars = asci.GetChars(hash);

            int y=0;
            
            while (y != settings.wordLen)
            {
                for (int k = 0; k < hash.Length; k++)
                {
                    hash[k] += (byte)(k ^ poz);
                    if (settings.charSet.Contains<char>((char)hash[k]))
                    {
                        if (y == settings.wordLen)
                        {
                            return word;
                        }
                        
                        word[y] = hash[k];
                        y++;
                    }
                }
            }
            return word;
        }

        
        
        
        public void BuildTable(byte[] startWord)
        {
            if (startWord.Length != settings.wordLen)
            {
                throw new ArgumentException("The Length of the starting word doesnt match the length specified in the settings obj");
            }


            //vytvorime meno tabulky
            //tvar nazvuje tb_dlzkaSlova_pociatocneSlovo.rtb
            string tableName = "tb_" + settings.wordLen + "_" + ByteArrayToString(startWord) + ".rtb";

            if (File.Exists(settings.tablePath + tableName))
            {
                throw new FileAlreadyExistsException("A table with the same settings already exists");
            }
            else
            {
                file = new FileStream(settings.tablePath + tableName, FileMode.Create, FileAccess.Write, FileShare.None);

            }

            //najpr generujeme pre prve vychodiskove slovo
            this.CreateChain(ref chain, startWord);
            rowOfChains.Add(chain[0].word, chain[chain.Count - 1].hash); // pridame prvy riadok
            int dumpCount = 0;

            //test only
            ASCIIEncoding asci = new ASCIIEncoding();

            
            byte[] actualWord = new byte[startWord.Length];
            comb = new Combinations(settings.charSet, startWord.Length);

            while (comb.MoveNext())
            {

                for (int i = 0; i < ((object[])comb.Current).Length; i++)
                {
                   char tmp = (char)((object[])comb.Current)[i];
                   actualWord[i] = (byte)tmp;
                }

                /*****************BUGGY PART******************************/
                
                chain = new List<Piece>();
                this.CreateChain(ref chain, actualWord);
                Piece piece01 = new Piece();
                Piece piece02 = new Piece();
                piece01 = chain[0];
                piece02 = chain[chain.Count - 1];
                /*********************************************************/
                if(!rowOfChains.ContainsKey(piece01.word))
                {
                    
                    rowOfChains.Add(piece01.word,piece02.hash);
                }
                else
                {
                    continue;
                }

                Console.WriteLine(asci.GetString(chain[0].word) + " - " + Convert.ToBase64String(chain[chain.Count - 1].hash));

                if (Report != null)
                {
                    Report(this, new ReportEventArgument(rowOfChains.Count, chain.Count));
                }

                dumpCount++;
                if (dumpCount == settings.dumpInterval)
                {
                    this.WriteToFile(ref rowOfChains);
                    dumpCount = 0;
                }
            }

            file.Close();
        }