bencoding 1.0.0 bencoding: ^1.0.0 copied to clipboard
A dart library for encoding and decoding bencoded data, according to the BitTorrent specification.
Bencoding #
A dart library for encoding and decoding bencoded data, according to the BitTorrent specification.
Index #
About BEncoding #
from Wikipedia:
Bencode (pronounced like B encode) is the encoding used by the peer-to-peer file sharing system BitTorrent for storing and transmitting loosely structured data.
It supports four different types of values:
- byte strings
- integers
- lists
- dictionaries
Bencoding is most commonly used in torrent files. These metadata files are simply bencoded dictionaries.
Usage #
Import #
import 'package:bencoding/bencode.dart' as bencode;
Encoding #
final data = {
'string': 'Hello World',
'integer': 12345,
'dict': {
'key': 'This is a string within a dictionary',
},
'list': [ 1, 2, 3, 4, 'string', 5, {} ],
};
final buffer = bencode.encode(data); // Return a Uint8List
final string = bencode.encodeToString(data); // Return a String
print('Buffer: $buffer');
print('String: $string');
Output
Buffer: [100, 54, 58, 115, 116, 114, 105, 110, 103, 49, 49, ...]
String: d6:string11:Hello World7:integeri12345e4:dictd3:key36:This is ...
Decoding #
final bencodedData = 'd6:string11:Hello World7:integeri12345e4:dictd3:key36:This is a string within a dictionarye4:listli1ei2ei3ei4e6:stringi5edeee';
print(bencode.decodeString(bencodedData));
// or you can use bencode.deocode(data) if your input data is a buffer (Uint8List)
Output
{
'string': 'Hello World',
'integer': 12345,
'dict': {
'key': 'This is a string within a dictionary',
},
'list': [ 1, 2, 3, 4, 'string', 5, {}],
}
API #
encode( dynamic
data ) #
String
|int
|List
|Map
data
Returns Uint8List
encodeToString ( dynamic
data ) #
String
|int
|List
|Map
data
Returns String
decode( Uint8List
data ) #
Returns dynamic
(String
| int
| List
| Map
)
decodeString ( String
data ) #
Returns dynamic
(String
| int
| List
| Map
)