Exemplo n.º 1
0
        static byte[] calculate_hash(string input)
        {
            byte[] input_bytes = get_length_seq(input);

            // Initialize the queue
            FlipCircleQueue fcq = apply_operations(input_bytes);

            return(extract_hash(fcq));
        }
Exemplo n.º 2
0
        static byte[] extract_hash(FlipCircleQueue fcq)
        {
            Node node = fcq.head;

            byte[] sums = new byte[QUEUE_SIZE / 16];

            for (int i = 0; i < QUEUE_SIZE; i++)
            {
                sums [i / 16] = (byte)(sums [i / 16] ^ (byte)node.value);

                node = fcq.get_next(node);
            }
            return(sums);
        }
Exemplo n.º 3
0
        static string extract_hash(FlipCircleQueue fcq)
        {
            Node node = fcq.head;

            byte[] sums = new byte[QUEUE_SIZE / 16];

            for (int i = 0; i < QUEUE_SIZE; i++)
            {
                sums [i / 16] = (byte)(sums [i / 16] ^ (byte)node.value);

                node = fcq.get_next(node);
            }
            return(BitConverter.ToString(sums).Replace("-", "").ToLower());
        }
Exemplo n.º 4
0
        public void merge(FlipCircleQueue other)
        {
            Node node = other.head;

            while (true)
            {
                Node next_node = other.get_next(node);
                this.add_node(node);

                if (next_node == other.head)
                {
                    break;
                }
                node = next_node;
            }
        }
Exemplo n.º 5
0
        static FlipCircleQueue apply_operations(byte[] input_bytes)
        {
            FlipCircleQueue fcq = new FlipCircleQueue();

            for (int i = 0; i < QUEUE_SIZE; i++)
            {
                fcq.add_node(i);
            }
            uint skip_size = 0;

            // Once we move the queue head back and force, we need to keep
            // track of the original start position for the final calculation.
            long start_pos = 0;

            for (int iteration = 0; iteration < 64; iteration++)
            {
                foreach (uint l in input_bytes)
                {
                    if (l > 0)
                    {
                        FlipCircleQueue cut = fcq.cut(fcq.head, l);
                        cut.flip();
                        cut.reset_head(cut.n_from_head(1));
                        fcq.merge(cut);
                    }

                    fcq.reset_head(fcq.n_from_head(skip_size));
                    start_pos = (QUEUE_SIZE + start_pos - l - skip_size) % QUEUE_SIZE;
                    skip_size++;
                }
            }

            // Calculate the hash
            start_pos = (start_pos + QUEUE_SIZE) % QUEUE_SIZE;

            fcq.reset_head(fcq.n_from_head((uint)start_pos));
            return(fcq);
        }
Exemplo n.º 6
0
        public FlipCircleQueue cut(Node start, uint len)
        {
            Node end       = n_from_node(start, len - 1);
            Node pre_start = get_prev(start);
            Node post_end  = get_next(end);

            if (start == post_end)
            {
                this.head = null;
            }
            else
            {
                set_prev(post_end, pre_start);
                this.head = post_end;
            }


            FlipCircleQueue new_queue = new FlipCircleQueue(
                start, end, this.next_direction
                );

            return(new_queue);
        }