When the cksum calculates its output, it would append a byte stream of octal numbers representing the length of the original input. However, using CRC_32_CKSUM alone does not imitate the behaviour and some additional logic is needed to get the same output as cksum.
A test program:
use crc;
const CKSUM: crc::Crc<u32> = crc::Crc::<u32>::new(&crc::CRC_32_CKSUM);
fn main() {
let arg = std::env::args().nth(1).unwrap();
let input = arg.as_bytes();
let mut digest = CKSUM.digest();
digest.update(input);
let mut i = input.len();
while i > 0 {
let len_oct: u8 = (i & 0xFF).try_into().unwrap();
digest.update(&[len_oct]);
i >>= 8;
}
println!("No length: {}, has length: {}", CKSUM.checksum(input), digest.finalize());
}
Result:
> cargo run 123456789
No length: 1985902208, has length: 930766865
> printf 123456789 | cksum
930766865 9
When the
cksum
calculates its output, it would append a byte stream of octal numbers representing the length of the original input. However, usingCRC_32_CKSUM
alone does not imitate the behaviour and some additional logic is needed to get the same output ascksum
.A test program:
Result:
Should this be considered a bug or a feature?