| 1269 | |
| 1270 | |
| 1271 | == Vala == |
| 1272 | {{{ |
| 1273 | public uint64 hash(File file) { |
| 1274 | try { |
| 1275 | uint64 h; |
| 1276 | |
| 1277 | //get filesize and add it to hash |
| 1278 | var file_info = file.query_info("*", FileQueryInfoFlags.NONE); |
| 1279 | h = file_info.get_size(); |
| 1280 | |
| 1281 | //add first 64kB of file to hash |
| 1282 | var dis = new DataInputStream(file.read()); |
| 1283 | dis.set_byte_order(DataStreamByteOrder.LITTLE_ENDIAN); |
| 1284 | for(int i=0; i<65536/sizeof(uint64); i++) { |
| 1285 | h += dis.read_uint64(); |
| 1286 | } |
| 1287 | //add last 64kB of file to hash |
| 1288 | dis = new DataInputStream(file.read()); |
| 1289 | dis.set_byte_order(DataStreamByteOrder.LITTLE_ENDIAN); |
| 1290 | dis.skip((size_t)(file_info.get_size() - 65536)); |
| 1291 | for(int i=0; i<65536/sizeof(uint64); i++) { |
| 1292 | h += dis.read_uint64(); |
| 1293 | } |
| 1294 | |
| 1295 | return h; |
| 1296 | } catch (Error e) { |
| 1297 | error("%s", e.message); |
| 1298 | } |
| 1299 | } |
| 1300 | |
| 1301 | int main () { |
| 1302 | var file = File.new_for_path ("breakdance.avi"); |
| 1303 | if (!file.query_exists ()) { |
| 1304 | stderr.printf ("File '%s' doesn't exist.\n", file.get_path ()); |
| 1305 | return 1; |
| 1306 | } |
| 1307 | stdout.printf("%016llx\n", hash(file)); |
| 1308 | |
| 1309 | file = File.new_for_path ("dummy.bin"); |
| 1310 | if (!file.query_exists ()) { |
| 1311 | stderr.printf ("File '%s' doesn't exist.\n", file.get_path ()); |
| 1312 | return 1; |
| 1313 | } |
| 1314 | stdout.printf("%016llx\n", hash(file)); |
| 1315 | |
| 1316 | return 0; |
| 1317 | } |
| 1318 | }}} |
| 1319 | Build with: valac --pkg gio-2.0 hash.vala |