Ciao,
ho scritto un encoder in python e in Java; fanno le stesse identiche computazioni, e, se alimentati con lo stesso input, non forniscono lo stesso output.

In allegato trovate sia il codice Java che quello python.
Per iniziare, vi serve un file di input:
$ dd if=/dev/urandom of=test.img bs=1048576 count=1

Come potete vedere il codice è molto corto, ma i due programmi fanno esattamente la stessa cosa (la bitlist è uguale, quindi entrambi selezionano gli stessi blocchi in input e ne fanno lo xor - potete vedere che i blocchi selezionati corrispondono sempre perché ogni volta che un blocco viene selezionato ne stampa l'hash). Qui viene la parte di incongruenza: avendo gli stessi input, perché i due programmi producono due output diversi? Potete vedere questo dall'hash del blocco risultante (random_block) che viene stampato alla fine del programma.
In Java ha un valore che è totalmente diverso da quello in python. Perché?
Operando sullo stesso input e facendo le stesse operazioni, dovrebbero restituire lo stesso output.

Dove sbaglio?

Vi ringrazio

import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Test {
        int N;

        public byte[] create_random_block(byte[] piece)
                        throws NoSuchAlgorithmException, 
UnsupportedEncodingException {
                N = piece.length / 16384;
                byte[] random_block = new byte[16384];
                int[] bitlist = { 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 
1, 1, 0,
                                0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 
1, 1, 1, 0, 0,
                                1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 
0, 1, 0, 0, 1,
                                0, 1, 0, 1 };
                for (int i = 0; i < N; i++) {
                        if (bitlist[i] == 1) {
                                byte[] block = new byte[16384];
                                for (int j = 0; j < block.length; j++) {
                                        block[j] = piece[i * 16384 + j];
                                }
                                System.out.println(i * 16384 + "-" + (i * 16384 
+ 16384)
                                                + "   " + 
AeSimpleSHA1.SHA1(block));
                                for (int j = 0; j < random_block.length; j++) {
                                        random_block[j] = (byte) 
(random_block[j] ^ block[j]);
                                }
                        }
                }
                System.out.println(AeSimpleSHA1.SHA1(random_block));
                return random_block;
        }

        public static void main(String[] args) throws IOException,
                        NoSuchAlgorithmException {
                byte data[] = new byte[1024 * 1024];
                FileInputStream fi = new FileInputStream("test.img");
                fi.read(data);
                Test x = new Test();
                x.create_random_block(data);
                System.exit(0);

        }

        public static class AeSimpleSHA1 {
                private static String convertToHex(byte[] data) {
                        StringBuffer buf = new StringBuffer();
                        for (int i = 0; i < data.length; i++) {
                                int halfbyte = (data[i] >>> 4) & 0x0F;
                                int two_halfs = 0;
                                do {
                                        if ((0 <= halfbyte) && (halfbyte <= 9))
                                                buf.append((char) ('0' + 
halfbyte));
                                        else
                                                buf.append((char) ('a' + 
(halfbyte - 10)));
                                        halfbyte = data[i] & 0x0F;
                                } while (two_halfs++ < 1);
                        }
                        return buf.toString();
                }

                public static String SHA1(byte[] text) throws 
NoSuchAlgorithmException,
                                UnsupportedEncodingException {
                        MessageDigest md;
                        md = MessageDigest.getInstance("SHA-1");
                        byte[] sha1hash = new byte[40];
                        md.update(text);
                        sha1hash = md.digest();
                        return convertToHex(sha1hash);
                }
        }

}
import os
import sha
import sys

class EncoderDecoder(object):
    def create_random_block(self,piece,blocksize=16384):
        if len(piece) % blocksize != 0:
            raise Exception('size error')
        self.N = len(piece)/blocksize
        random_block = ['0']*blocksize
        bitlist = [1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 
1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 
1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1]
        for i in range(len(bitlist)):
            if bitlist[i] == 1:
                block = piece[i*blocksize:i*blocksize+blocksize]
                print '%d-%d   %s' 
%(i*blocksize,i*blocksize+blocksize,sha.new(block).hexdigest())
                for j in range(blocksize):
                    random_block[j] = chr(ord(random_block[j]) ^ ord(block[j]))
        print sha.new(''.join(random_block)).hexdigest()
        return ''.join(random_block)
   
if __name__ == '__main__':
    data = open('test.img','rb').read()
    x = EncoderDecoder()
    x.create_random_block(data)
    sys.exit(0)
_______________________________________________
Python mailing list
Python@lists.python.it
http://lists.python.it/mailman/listinfo/python

Rispondere a