Changes between Version 65 and Version 66 of HashSourceCodes


Ignore:
Timestamp:
Jul 25, 2016, 4:37:34 AM (8 years ago)
Author:
os
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • HashSourceCodes

    v65 v66  
    44Hash code is based on [http://sourceforge.net/projects/guliverkli/ Media Player Classic]. In natural language it calculates: size + 64bit chksum of the first and last 64k (even if they overlap because the file is smaller than 128k). On opensubtitles.org is movie file size limited to '''9000000000 > $moviebytesize > 131072 bytes''', if is there any reason to change these sizes, let us know. Licence of [http://guliverkli.svn.sourceforge.net/viewvc/guliverkli/ hashing source codes] is [http://sourceforge.net/projects/guliverkli/ GPL]. Source codes was tested on Little Endian - DEC, Intel and compatible
    55
    6 Important: there might be cases, when your calculated hash is not 16 characters, so make sure you add zero-leading padding - some of source codes doesn't implement this ('101eae5380a4769' => '0101eae5380a4769').
     6Important: there might be cases, when your calculated hash is not 16 characters, so make sure you add zero-leading padding - some of source codes doesn't implement this ('101eae5380a4769' => '0101eae5380a4769').
    77
    88Feel free to edit/add source-codes if you have faster/better implementation. Also don't forget to check, if hash is right for test. '''Test these 2 files please to ensure your algo is completely OK''' (otherwise you can poison the database and that nobody wants): 
     
    19681968///println(result.fileSize)
    19691969}}}
     1970
     1971
     1972== RUST ==
     1973{{{
     1974use std::fs;
     1975use std::fs::File;
     1976use std::io::{Read, Seek, SeekFrom, BufReader};
     1977use std::mem;
     1978
     1979const HASH_BLK_SIZE: u64 = 65536;
     1980
     1981fn create_hash(file: File, fsize: u64) -> Result<String, std::io::Error> {
     1982
     1983    let mut buf = [0u8; 8];
     1984    let mut word: u64;
     1985
     1986    let mut hash_val: u64 = fsize;  // seed hash with file size
     1987
     1988    let iterations = HASH_BLK_SIZE /  8;
     1989
     1990    let mut reader = BufReader::with_capacity(HASH_BLK_SIZE as usize, file);
     1991
     1992    for _ in 0..iterations {
     1993        try!(reader.read(&mut buf));
     1994        unsafe { word = mem::transmute(buf); };
     1995        hash_val = hash_val.wrapping_add(word);
     1996    }
     1997
     1998    try!(reader.seek(SeekFrom::Start(fsize - HASH_BLK_SIZE)));
     1999
     2000    for _ in 0..iterations {
     2001        try!(reader.read(&mut buf));
     2002        unsafe { word = mem::transmute( buf); };
     2003        hash_val = hash_val.wrapping_add(word);
     2004    }
     2005
     2006    let hash_string = format!("{:01$x}", hash_val, 16);
     2007
     2008    Ok(hash_string)
     2009}
     2010
     2011fn main() {
     2012    let fname = "breakdance.avi";
     2013    let fsize = fs::metadata(fname).unwrap().len();
     2014    if fsize>HASH_BLK_SIZE {
     2015        let file = File::open(fname).unwrap();
     2016        let fhash = create_hash(file, fsize).unwrap();
     2017        println!("Hash for {} is  {}", fname, fhash);
     2018    }
     2019}
     2020}}}