crc24-0.1.6/.gitignore000060000000000000000000000000271246317267400126140ustar0000000000000000/target /Cargo.lock *~ crc24-0.1.6/.travis.yml000066400000000000000000000007761247437640500127620ustar0000000000000000language: rust script: - cargo build --verbose - cargo test --verbose - cargo doc after_success: > [ $TRAVIS_BRANCH = master ] && [ $TRAVIS_PULL_REQUEST = false ] && mv target/doc . && curl http://www.rust-ci.org/artifacts/put?t=$RUSTCI_TOKEN | sh env: global: - secure: Ji6hWfugbi9TptKYiqnb8MhtgZfyLGWsmXqqdzpOsn1OcPd3UFck0+xHASRWk2LHWj1TqKXzX9sQahf+hHfFsifTbKqN4xo25gUzZ3SQKe6XQ/Z5CX/5QERiQyzxdBGpSuREXJeh34C6W71Dw+F6XVL/TWNKYhja139faMrS5h8= notifications: email: on_success: never crc24-0.1.6/Cargo.toml000066400000000000000000000017031250757751100125660ustar0000000000000000# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] name = "crc24" version = "0.1.6" authors = ["Sebastian Gesemann "] build = "src/tablegen.rs" description = "CRC-24 implementation (IETF RFC2440-compatible)" homepage = "https://github.com/sellibitze/crc24-rs" documentation = "http://rust-ci.org/sellibitze/crc24-rs/doc/crc24/" readme = "README.md" keywords = [ "CRC-24", "CRC24", "CRC", "checksum", "hash", ] license = "MIT/Apache-2.0" repository = "https://github.com/sellibitze/crc24-rs.git" crc24-0.1.6/Cargo.toml.orig000066400000000000000000000007311250757751100135250ustar0000000000000000[package] name = "crc24" version = "0.1.6" authors = ["Sebastian Gesemann "] license = "MIT/Apache-2.0" readme = "README.md" repository = "https://github.com/sellibitze/crc24-rs.git" homepage = "https://github.com/sellibitze/crc24-rs" documentation = "http://rust-ci.org/sellibitze/crc24-rs/doc/crc24/" description = "CRC-24 implementation (IETF RFC2440-compatible)" keywords = ["CRC-24", "CRC24", "CRC", "checksum", "hash"] build = "src/tablegen.rs" crc24-0.1.6/README.md000066400000000000000000000005401246321336600121070ustar0000000000000000# crc24-rs This library package (for programs written in the Rust language) provides an implementation of the CRC-24 checksum like it is specified in the IETF RFC2440. # Links * This package is also available at [crates.io](https://crates.io/crates/crc24). * You can find the documentation [here](http://rust-ci.org/sellibitze/crc24-rs/doc/crc24/). crc24-0.1.6/src/lib.rs000066400000000000000000000041531250757745000125450ustar0000000000000000//! Crate providing a CRC-24 hasher based on the IETF RFC2440 specification. use std::default::Default; use std::hash::Hasher; const INIT: u32 = 0xB7_04_CE; include! { concat!(env!("OUT_DIR"), "/table.inc") } /// CRC-24 hasher based on IETF RFC2440 specification. #[derive(Copy,Clone,PartialEq,Eq)] pub struct Crc24Hasher { state: u32 } impl Crc24Hasher { /// Creates a new CRC-24 hasher initialized with the given state. pub fn init(v: u32) -> Crc24Hasher { Crc24Hasher { state: v & 0xFF_FF_FF } } /// Creates a new CRC-24 hasher initialized with a nonzero state /// specified in RFC2440. pub fn new() -> Crc24Hasher { Crc24Hasher { state: INIT } } } impl Default for Crc24Hasher { /// Creates a new CRC-24 hasher initialized with a nonzero state /// specified in RFC2440. fn default() -> Crc24Hasher { Crc24Hasher::new() } } impl Hasher for Crc24Hasher { fn finish(&self) -> u64 { self.state as u64 } fn write(&mut self, msg: &[u8]) { let mut s = self.state; for &octet in msg.iter() { let index = ((octet as u32) ^ (s >> 16)) & 0xFF; s = (s << 8) ^ CRC24_TABLE[index as usize]; } self.state = s & 0xFF_FF_FF; } } /// Computes hash of the raw bytes using CRC-24 /// (without including the length as part of the data) pub fn hash_raw(octets: &[u8]) -> u32 { let mut h: Crc24Hasher = Default::default(); h.write(octets); h.finish() as u32 } #[cfg(test)] mod test { const CRC24_INIT: u32 = 0x__b7_04_ce; const CRC24_POLY: u32 = 0x1_86_4c_fb; // including x^24 // directly translated from RFC2440 section 6.1. fn crc_octets(octets: &[u8]) -> u32 { let mut crc = CRC24_INIT; for &octet in octets.iter() { crc ^= (octet as u32) << 16; for _ in 0..8 { crc <<= 1; if (crc & 0x1_00_00_00) != 0 { crc ^= CRC24_POLY; } } } crc & 0xFF_FF_FF } fn test_compare_impls(octets: &[u8]) -> bool { let h1 = crc_octets(octets); let h2 = super::hash_raw(octets); h1 == h2 } #[test] fn test() { assert!(test_compare_impls(b"")); assert!(test_compare_impls(b"x")); assert!(test_compare_impls(b"sg")); assert!(test_compare_impls(b"crc")); assert!(test_compare_impls(b"test")); } } // mod test crc24-0.1.6/src/tablegen.rs000066400000000000000000000017651250427524100135540ustar0000000000000000use std::env; use std::fs; use std::io::prelude::*; use std::path; const CRC24_POLY: u32 = 0x86_4C_FB; // CRC-24 (IETF RFC2440), used by OpenPGP const INC_FILE: &'static str = "table.inc"; fn main() { let ods = env::var("OUT_DIR").unwrap(); let odp = path::Path::new(&ods); let f = &mut fs::File::create(&odp.join(INC_FILE)).unwrap(); write!(f, "{}", into_code(table_gen())).unwrap(); } fn table_gen() -> Vec { let mut v = Vec::new(); for hi in 0..256u32 { let mut temp = hi << 16; for _ in 0..8 { let x = if (temp & 0x80_00_00) == 0 { 0 } else { CRC24_POLY }; temp = ((temp & 0x7F_FF_FF) << 1) ^ x; } v.push(temp); } v } fn into_code(tab: Vec) -> String { let mut out: Vec = Vec::new(); writeln!(&mut out, "const CRC24_TABLE: [u32; 256] = [").unwrap(); for row in tab.chunks(4) { writeln!(&mut out, "\t0x{:06x}, 0x{:06x}, 0x{:06x}, 0x{:06x},", row[0], row[1], row[2], row[3]).unwrap(); } writeln!(&mut out, "];").unwrap(); String::from_utf8(out).unwrap() }