Skip to content

Commit de6680b

Browse files
committed
hashing function
test suite initialised samples package
1 parent 283ccf8 commit de6680b

File tree

6 files changed

+163
-0
lines changed

6 files changed

+163
-0
lines changed

lib/subdb.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
var fs = require('fs'),
2+
crypto = require('crypto'),
3+
ee = require('events').EventEmitter
4+
//,
5+
//Api = require('./endpoints.js')
6+
;
7+
8+
9+
var os = module.exports = function() {
10+
/*
11+
this.api = new Api();
12+
13+
*/
14+
};
15+
16+
os.prototype.__proto__ = ee.prototype;
17+
18+
/*
19+
os.prototype.checkMovieHash = function(t_hash, cb) {
20+
21+
};
22+
*/
23+
os.prototype.computeHash = function(path, cb) {
24+
// get first 64kb
25+
// get last 64kb
26+
// md5 everything
27+
28+
var file_size = 0;
29+
var chunk_size = 65536;
30+
var buf = new Buffer(chunk_size*2);
31+
var b_read = 0;
32+
33+
fs.stat(path, function(err, stat){
34+
if(err) return cb(err);
35+
36+
file_size = stat.size;
37+
38+
fs.open(path, 'r', function(err, fd) {
39+
if(err) return cb(err);
40+
41+
var t_offsets = [0, file_size-chunk_size];
42+
for(var i in t_offsets) {
43+
b_read = fs.readSync(fd, buf, b_read, chunk_size, t_offsets[i]);
44+
}
45+
46+
var md5sum = crypto.createHash('md5');
47+
md5sum.update(buf);
48+
var d = md5sum.digest('hex');
49+
50+
cb(null, d);
51+
});
52+
});
53+
};
54+
55+

package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"author": "ka2er <[email protected]>",
3+
"name": "subdb",
4+
"description": "API to interact with subdb services",
5+
"version": "0.0.1",
6+
"repository": {
7+
"type": "git",
8+
"url": "git://github.com/ka2er/node-subdb-api.git"
9+
},
10+
"bugs": {
11+
"url" : "http://github.com/ka2er/node-subdb-api/issues"
12+
},
13+
"scripts": {
14+
"test": "mocha"
15+
}, "main": "lib/subdb.js",
16+
"dependencies": {
17+
18+
},
19+
"devDependencies": {
20+
"mocha":"1.6.0"
21+
},
22+
"optionalDependencies": {},
23+
"engines": {
24+
"node": "*"
25+
},
26+
"keywords": [
27+
"subdb",
28+
"imdb"
29+
],
30+
"license": "GPL"
31+
}

sample.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import os
2+
import hashlib
3+
4+
#this hash function receives the name of the file and returns the hash code
5+
def get_hash(name):
6+
readsize = 64 * 1024
7+
with open(name, 'rb') as f:
8+
data = f.read(readsize)
9+
print "%s" % hashlib.md5(data).hexdigest()
10+
#print "%0x16%" % data
11+
f.seek(-readsize, os.SEEK_END)
12+
data2 = f.read(readsize)
13+
print "%s" % hashlib.md5(data2).hexdigest()
14+
data += data2
15+
return hashlib.md5(data).hexdigest()
16+
17+
18+
print "%s" % get_hash('./test/dexter.mp4')

test/dexter.mp4

2.97 MB
Binary file not shown.

test/justified.mp4

8.85 MB
Binary file not shown.

test/test.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* User: seb
3+
* Date: 03/10/12
4+
* Time: 10:07
5+
* To change this template use File | Settings | File Templates.
6+
*/
7+
8+
var assert = require("assert");
9+
OS = require("../lib/subdb.js");
10+
11+
describe('Subdb', function() {
12+
13+
describe('#computeHash()', function(){
14+
15+
it('should return a subdb hash of the dexter sample file ', function(done){
16+
var os = new OS();
17+
os.computeHash(process.cwd()+'/test/dexter.mp4', function(err, size){
18+
if (err) return done(err);
19+
assert.equal(size, 'ffd8d4aa68033dc03d1c8ef373b9028c');
20+
done();
21+
});
22+
});
23+
24+
it('should return a opensubtitles hash of the justifier sample file ', function(done){
25+
var os = new OS();
26+
os.computeHash(process.cwd()+'/test/justified.mp4', function(err, size){
27+
if (err) return done(err);
28+
assert.equal(size, 'edc1981d6459c6111fe36205b4aff6c2');
29+
done();
30+
});
31+
});
32+
33+
34+
it('should return a 128bits length opensubtitles hash of the movie file ', function(done){
35+
var os = new OS();
36+
os.computeHash(process.cwd()+'/test/justified.mp4', function(err, size){
37+
if (err) return done(err);
38+
assert.equal(size.length, 'edc1981d6459c6111fe36205b4aff6c2'.length);
39+
done();
40+
});
41+
});
42+
43+
});
44+
/*
45+
describe('#checkMovieHash', function() {
46+
it('should identify justified trailer', function(done) {
47+
this.timeout(4000);
48+
var os = new OS();
49+
os.checkMovieHash(['edc1981d6459c6111fe36205b4aff6c2'], function(err, res) {
50+
if(err) return done(err);
51+
52+
assert.equal(res.data['edc1981d6459c6111fe36205b4aff6c2'], '???');
53+
done();
54+
});
55+
});
56+
});
57+
*/
58+
});
59+

0 commit comments

Comments
 (0)