feat: add I2P BitTorrent engine, DHT, and Firefox torrent extension
- Add pure-MoonBit bittorrent package: bencode codec, metainfo/magnet parsing with path traversal checks, peer wire protocol (BEP 10/9), and I2P Kademlia DHT with KRPC and 54-byte compact nodes - Add SHA-1 primitive in crypto for torrent info hashes - Add router torrent API and engine, enabled with --torrent-api (default 127.0.0.1:4445) and --download-dir - Add Firefox WebExtension torrent manager with native messaging host and packed .xpi - Add scripts to build a standalone Firefox + router AppImage and install the native host - Add just targets (proxy, appimage, extension), README docs, and ignore .cache/ and dist/
This commit is contained in:
1 parent
14eae00f5c
commit
fbe6818c4e
37 files changed
+8543
-9
No files matched your search
+123
@@ -0,0 +1,123 @@
|
||||
///|
|
||||
/// Pure-MoonBit FIPS 180-4 / RFC 3174 SHA-1 implementation
|
||||
/// (used for BitTorrent `info_hash`, piece verification, and BEP 5 Kademlia IDs).
|
||||
|
||||
///|
|
||||
fn sha1_rotl32(x : UInt32, n : Int32) -> UInt32 {
|
||||
(x << n) | (x >> (32 - n))
|
||||
}
|
||||
|
||||
///|
|
||||
/// Incremental SHA-1 hasher (FIPS 180-4).
|
||||
pub struct Sha1 {
|
||||
state : FixedArray[UInt32]
|
||||
block : FixedArray[Byte]
|
||||
mut block_len : Int32
|
||||
mut total_len : UInt64
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn Sha1::new() -> Sha1 {
|
||||
{
|
||||
state: [0x67452301U, 0xefcdab89U, 0x98badcfeU, 0x10325476U, 0xc3d2e1f0U],
|
||||
block: FixedArray::make(64, b'\x00'),
|
||||
block_len: 0,
|
||||
total_len: 0UL,
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn Sha1::compress(self : Sha1) -> Unit {
|
||||
let w = FixedArray::make(80, 0U)
|
||||
for i = 0; i < 16; i = i + 1 {
|
||||
let j = i * 4
|
||||
w[i] = (self.block[j].to_uint() << 24) |
|
||||
(self.block[j + 1].to_uint() << 16) |
|
||||
(self.block[j + 2].to_uint() << 8) |
|
||||
self.block[j + 3].to_uint()
|
||||
}
|
||||
for i = 16; i < 80; i = i + 1 {
|
||||
w[i] = sha1_rotl32(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1)
|
||||
}
|
||||
let mut a = self.state[0]
|
||||
let mut b = self.state[1]
|
||||
let mut c = self.state[2]
|
||||
let mut d = self.state[3]
|
||||
let mut e = self.state[4]
|
||||
for i = 0; i < 80; i = i + 1 {
|
||||
let (f, k) = if i < 20 {
|
||||
((b & c) | (b.lnot() & d), 0x5a827999U)
|
||||
} else if i < 40 {
|
||||
(b ^ c ^ d, 0x6ed9eba1U)
|
||||
} else if i < 60 {
|
||||
((b & c) | (b & d) | (c & d), 0x8f1bbcdcU)
|
||||
} else {
|
||||
(b ^ c ^ d, 0xca62c1d6U)
|
||||
}
|
||||
let temp = sha1_rotl32(a, 5) + f + e + k + w[i]
|
||||
e = d
|
||||
d = c
|
||||
c = sha1_rotl32(b, 30)
|
||||
b = a
|
||||
a = temp
|
||||
}
|
||||
self.state[0] = self.state[0] + a
|
||||
self.state[1] = self.state[1] + b
|
||||
self.state[2] = self.state[2] + c
|
||||
self.state[3] = self.state[3] + d
|
||||
self.state[4] = self.state[4] + e
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn Sha1::update(self : Sha1, data : Bytes) -> Unit {
|
||||
let len = data.length()
|
||||
self.total_len = self.total_len + len.to_uint64()
|
||||
for i = 0; i < len; i = i + 1 {
|
||||
self.block[self.block_len] = data[i]
|
||||
self.block_len += 1
|
||||
if self.block_len == 64 {
|
||||
self.compress()
|
||||
self.block_len = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn Sha1::finalize(self : Sha1) -> Bytes {
|
||||
let bit_len = self.total_len * 8UL
|
||||
self.block[self.block_len] = b'\x80'
|
||||
self.block_len += 1
|
||||
if self.block_len > 56 {
|
||||
while self.block_len < 64 {
|
||||
self.block[self.block_len] = b'\x00'
|
||||
self.block_len += 1
|
||||
}
|
||||
self.compress()
|
||||
self.block_len = 0
|
||||
}
|
||||
while self.block_len < 56 {
|
||||
self.block[self.block_len] = b'\x00'
|
||||
self.block_len += 1
|
||||
}
|
||||
for i = 0; i < 8; i = i + 1 {
|
||||
self.block[56 + i] = ((bit_len >> (56 - i * 8)) & 0xFFUL).to_byte()
|
||||
}
|
||||
self.compress()
|
||||
let out = FixedArray::make(20, b'\x00')
|
||||
for i = 0; i < 5; i = i + 1 {
|
||||
let v = self.state[i]
|
||||
out[i * 4] = ((v >> 24) & 0xFFU).to_byte()
|
||||
out[i * 4 + 1] = ((v >> 16) & 0xFFU).to_byte()
|
||||
out[i * 4 + 2] = ((v >> 8) & 0xFFU).to_byte()
|
||||
out[i * 4 + 3] = (v & 0xFFU).to_byte()
|
||||
}
|
||||
Bytes::from_array(out)
|
||||
}
|
||||
|
||||
///|
|
||||
/// Computes the 20-byte SHA-1 digest of `data`.
|
||||
pub fn sha1(data : Bytes) -> Bytes {
|
||||
let h = Sha1::new()
|
||||
h.update(data)
|
||||
h.finalize()
|
||||
}
|
||||
Reference in new issue
Block a user