Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
A Python library for Bencode encoding and decoding.
Installing through pip:
pip install libbencode
Installing through source:
git clone https://github.com/zrekryu/libbencode.git
cd libbencode
pip install .
import libbencode
data: bytes = b'i42e' # bencode integer.
decoded_data: int = libbencode.decode(data)
print(decoded_data) # 42
import libbencode
data: int = 42 # python integer.
encoded_data: bytes = libbencode.encode(data)
print(encoded_data) # b'i42e'
To print the version of the libbencode
library:
import libbencode
print(libbencode.__version__)
All decode_*
methods return a tuple of the decoded data and the position of the end byte in the data.
Decodes an integer from bencode format:
from libbencode import Decoder
data: bytes = b"i42e"
decoded_data: tuple[int, int] = Decoder.decode_int(data)
print(decoded_data) # (42, 3)
Decodes a string from bencode format:
from libbencode import Decoder
data: bytes = b"4:spam"
decoded_data: tuple[bytes, int] = Decoder.decode_str(data)
print(decoded_data) # (b'spam', 5)
Decoding with a specific encoding:
decoded_data: tuple[str, int] = Decoder.decode_str(data, encoding="utf-8") # ('spam', 5)
Decodes a list from bencode format:
from libbencode import Decoder
data: bytes = b"l4:spami42ee"
decoded_data: tuple[list[bytes | int], int] = Decoder.decode_list(data)
print(decoded_data) # ([b'spam', 42], 11)
Decoding with a specific encoding:
decoded_data: tuple[list[str | int], int] = Decoder.decode_list(data, encoding="utf-8") # (['spam', 42], 21)
Decodes a dictionary from bencode format:
from libbencode import Decoder
data: bytes = b"d3:bar4:spam3:fooi42ee"
decoded_data: tuple[dict[bytes, bytes | int]] = Decoder.decode_dict(data)
print(decoded_data) # ({b'bar': b'spam', b'foo': 42}, 21)
Decoding with a specific encoding:
decoded_data: tuple[dict[str, str | int], int] = Decoder.decode_dict(data, encoding="utf-8") # {'bar': 'spam', 'foo': 42}
from libbencode import Encoder
data: int = 42
encoded_data: bytes = Encoder.encode_int(data)
print(encoded_data) # b'i42e'
Encodes bytes into bencode format:
from libbencode import Encoder
data: bytes = b"spam"
encoded_data: bytes = Encoder.encode_bytes(data)
print(encoded_data) # b'4:spam'
Encodes a string into bencode format:
from libbencode import Encoder
data: str = "spam"
encoded_data: bytes = Encoder.encode_str(data)
print(encoded_data) # b'4:spam'
Encodes a list into bencode format:
from libbencode import Encoder
data: list[str | int] = ["spam", 42]
encoded_data: bytes = Encoder.encode_list(data)
print(encoded_data) # b'l4:spami42ee'
Encodes a dictionary into bencode format:
from libbencode import Encoder
data: dict[str, str | int] = {"bar": "spam", "foo": 42}
encoded_data: bytes = Encoder.encode_dict(data)
print(encoded_data) # b'd3:bar4:spam3:fooi42ee'
The Bencode format does not natively support boolean values. Therefore, booleans are encoded as integers:
b'i1e'
b'i0e'
Encodes a boolean into bencode format:
from libbencode import Encoder
data: bool = True
encoded_data: bytes = Encoder.encode_bool(data)
print(encoded_data) # b'i1e'
Tests are included in tests
directory.
Run tests:
python -m unittest discover -s tests
© 2024 Zrekryu. Licensed under MIT License. See the LICENSE file for details.
FAQs
Bencode encoder and decoder.
We found that libbencode demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.