<commit_before>bittorrent-nodeID
===

Javascript implementation of [BEP42 - DHT Security extension](http://www.bittorrent.org/beps/bep_0042.html) to calculate or check the nodeID of a peer.

BEP42 is implemented in projects uTorrent, libtorrent and bootstrap-dht.

## Installation and use

This is using [node-fast-crc32c](https://github.com/ashi009/node-fast-crc32c) from Xiaoyi and/or [sse4_crc32](https://github.com/Voxer/sse4_crc32) from Anand Suresh.

It does include fast-crc32c in node_modules slightly modified to handle the binary format (see the explaination below) but you can link to npm directly:

	npm install bittorrent-nodeid
	
Then:

	var generate_id=require('bittorent-nodeid');

	generate_id('W.X.Y.Z',Random_byte) --> prefix abcdefgh --> nodeID: (first 20 bits of prefix) (random number) Random_byte
	generate_id('172.16.31.10',1) --> prefix 5fbfbdb2 --> nodeID: 5fbfb e7519910c34ae7026c3e64eacc13c5159 01
	generate_id('192.168.3.11',86) --> prefix 5a3ce9b0 --> nodeID: 5a3ce 91f3dc70a057e9a9fe0cc900d52b4e61e 56
	generate_id('172.16.31.10',22) --> prefix a5d4344a --> a5d43 396da271c1a4d1dc7149247f021eabc34 16
	
## Note about the binary format

BEP42 does define the following calculation to compute the nodeID:

	crc32c((ip & 0x030f3fff) | (r << 29))

Where ip is the ip address representation in network byte order.

For ip 172.16.31.10, the calculation with a random number set to 1 will be ``crc32c((0x7c1f4b15 & 0x030f3fff) | (1 << 29))``, so ``crc32c(0x200f0b15)`` which is computed as ``crc32c('ABCD')`` where ABCD are the characters corresponding to the ascii code of each byte, the current implementations do process crc32c progessively byte by byte (then crc32c('A'), crc32c('B', previous result), etc).

In javascript a character outside of the normal ascii range like 'Á' will be interpreted as utf8 ``0xc381`` and ``crc32c.calculate('Á')`` will give ``b1cf5bcd``

But most of the c++ libraries does handle this byte by byte, so 'Á' will be interpreted as ``0xc1`` and ``crc32c.calculate('Á')`` will give ``639bf696``

This implementation does follow the existing ones (uTorrent, libtorrent, bootstrap-dht) using the second method.

## Related projects :

* [Ayms/torrent-live](https://github.com/Ayms/torrent-live)
* [Ayms/node-Tor](https://github.com/Ayms/node-Tor)
* [Ayms/iAnonym](https://github.com/Ayms/iAnonym)
* [Interception Detector](http://www.ianonym.com/intercept.html)
* [Ayms/abstract-tls](https://github.com/Ayms/abstract-tls)
* [Ayms/websocket](https://github.com/Ayms/websocket)
* [Ayms/node-typedarray](https://github.com/Ayms/node-typedarray)
* [Ayms/node-dom](https://github.com/Ayms/node-dom)
* [Ayms/node-bot](https://github.com/Ayms/node-bot)
* [Ayms/node-gadgets](https://github.com/Ayms/node-gadgets)<commit_msg>npm and binary<commit_after>bittorrent-nodeID
===

Javascript implementation of [BEP42 - DHT Security extension](http://www.bittorrent.org/beps/bep_0042.html) to calculate or check the nodeID of a peer.

BEP42 is implemented in projects uTorrent, libtorrent and bootstrap-dht.

## Installation and use

This is using [node-fast-crc32c](https://github.com/ashi009/node-fast-crc32c) from Xiaoyi and/or [sse4_crc32](https://github.com/Voxer/sse4_crc32) from Anand Suresh.

It does include fast-crc32c in node_modules slightly modified to handle the binary format (see the explaination below) but you can link to npm directly:

	npm install bittorrent-nodeid
	
Then:

	var generate_id=require('bittorent-nodeid');

	generate_id('W.X.Y.Z',Random_byte) --> prefix abcdefgh --> nodeID: (first 20 bits of prefix) (random number) Random_byte
	generate_id('172.16.31.10',1) --> prefix 5fbfbdb2 --> nodeID: 5fbfb e7519910c34ae7026c3e64eacc13c5159 01
	generate_id('192.168.3.11',86) --> prefix 5a3ce9b0 --> nodeID: 5a3ce 91f3dc70a057e9a9fe0cc900d52b4e61e 56
	generate_id('172.16.31.10',22) --> prefix a5d4344a --> a5d43 396da271c1a4d1dc7149247f021eabc34 16
	
## Note about the binary format

BEP42 does define the following calculation to compute the nodeID:

	crc32c((ip & 0x030f3fff) | (r << 29))

Where ip is the ip address representation in network byte order.

For ip 172.16.31.10, the calculation with a random number set to 1 will be ``crc32c((0x7c1f4b15 & 0x030f3fff) | (1 << 29))``, so ``crc32c(0x200f0b15)`` which is computed as ``crc32c(new Buffer('200f0b15','hex')`` or ``crc32c('ABCD')`` where ABCD are the characters corresponding to the ascii code of each byte, the current implementations do process crc32c progessively byte by byte (then crc32c('A'), crc32c('B', previous result), etc).

In javascript a character outside of the normal ascii range like 'Á' will be interpreted as utf8 ``0xc381`` and ``crc32c.calculate('Á')`` will give ``b1cf5bcd``

But most of the c++ libraries does handle this byte by byte, so 'Á' will be interpreted as ``0xc1`` and ``crc32c.calculate('Á')`` will give ``639bf696``

This implementation does follow the existing ones (uTorrent, libtorrent, bootstrap-dht) using the second method.

## Related projects :

* [Ayms/torrent-live](https://github.com/Ayms/torrent-live)
* [Ayms/node-Tor](https://github.com/Ayms/node-Tor)
* [Ayms/iAnonym](https://github.com/Ayms/iAnonym)
* [Interception Detector](http://www.ianonym.com/intercept.html)
* [Ayms/abstract-tls](https://github.com/Ayms/abstract-tls)
* [Ayms/websocket](https://github.com/Ayms/websocket)
* [Ayms/node-typedarray](https://github.com/Ayms/node-typedarray)
* [Ayms/node-dom](https://github.com/Ayms/node-dom)
* [Ayms/node-bot](https://github.com/Ayms/node-bot)
* [Ayms/node-gadgets](https://github.com/Ayms/node-gadgets)
{'fraction_non_alphanumeric': 0.13600572655690765, 'fraction_numerical': 0.07838224767358626, 'mean_word_length': 5.048701298701299, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, 'https://': 22, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 8, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '271990', 'n_tokens_mistral': 2331, 'n_tokens_neox': 2034, 'n_words': 524}