Skip to content

Commit

Permalink
uses crc16-ccitt logic to create field crc16
Browse files Browse the repository at this point in the history
  • Loading branch information
naomijub committed Sep 14, 2020
1 parent da6b75a commit ae58407
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 5 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "brcode"
version = "1.0.1"
version = "1.1.0"
authors = ["Julia Naomi <[email protected]>"]
edition = "2018"
description = "Crate to parse and emit BR Codes"
Expand Down Expand Up @@ -37,4 +37,8 @@ harness = false

[[bench]]
name = "both_ways"
harness = false

[[bench]]
name = "crc16"
harness = false
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# BR Code

A crate to parse and emit [PIX BR Code](https://www.bcb.gov.br/content/estabilidadefinanceira/spb_docs/ManualBRCode.pdf).
* [Technical and Business specs for BR Code usage](https://www.bcb.gov.br/content/estabilidadefinanceira/forumpireunioes/Anexo%20I%20-%20QRCodes%20-%20Especifica%C3%A7%C3%A3o%20-%20vers%C3%A3o%201-1.pdf)


## Usage

```toml
[dependencies]
brcode = "1.0.1"
brcode = "1.1.0"
```

Shellscript to get files from release:
Expand Down Expand Up @@ -483,6 +485,11 @@ time: [33.238 us 33.555 us 33.924 us]
time: [24.537 us 25.391 us 26.260 us]
```

**crc16_ccitt** in `benches/crc16`:
```
time: [3.0738 us 3.0825 us 3.0938 us]
```

## Goals
- [x] Parse BR Code String;
- [x] Parse BR Code to `BrCode` struct;
Expand Down
15 changes: 15 additions & 0 deletions benches/crc16.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use brcode::crc16_ccitt;
use criterion::{criterion_group, criterion_main, Criterion};

fn criterion_benchmark(c: &mut Criterion) {
let message = message();
c.bench_function("crc16", |b| b.iter(|| crc16_ccitt(&message)));
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);

fn message() -> String {
"00020104141234567890123426580014BR.GOV.BCB.PIX0136123e4567-e12b-12d1-a456-42665544000027300012BR.COM.OUTRO011001234567895204000053039865406123.455802BR5917NOME DO RECEBEDOR6008BRASILIA61087007490062190515RP12345678-201980390012BR.COM.OUTRO01190123.ABCD.3456.WXYZ6304"
.to_string()
}
31 changes: 31 additions & 0 deletions src/aux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,29 @@ impl FromIterator<(usize, parse::Data)> for HashBrCode {
}
}

pub fn crc16_ccitt(message: &str) -> String {
let mut crc: u16 = 0xFFFF; // initial value
let polynomial: u16 = 0x1021; // 0001 0000 0010 0001 (0, 5, 12)

// byte[] testBytes = "123456789".getBytes("ASCII");

let bytes = message.as_bytes();

for b in bytes {
for i in 0u16..8u16 {
let bit = (b >> (7-i) & 1) == 1;
let c15 = (crc >> 15 & 1) == 1;
crc <<= 1;
if c15 ^ bit {crc ^= polynomial};
}
}

crc &= 0xffff;


format!("{:X}", crc)
}

#[cfg(test)]
mod test {
use super::*;
Expand All @@ -71,6 +94,14 @@ mod test {
assert_eq!(hash.0, expected);
}

#[test]
fn test_crc16_ccitt() {
let crc16 = crc16_ccitt("123456789");
let expected = "29B1";

assert_eq!(crc16, expected);
}

#[test]
fn creates_nested_map() {
let vec = vec![
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub(crate) mod parse;

pub use model::{BrCode, Info, Label, MerchantInfo, Template};
pub use parse::Data;
pub use aux::crc16_ccitt;

pub fn from_str(code: &str) -> Vec<(usize, parse::Data)> {
parse::parse(code, 99)
Expand Down
7 changes: 4 additions & 3 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,11 @@ impl BrCode {
m.info.iter().for_each(|i| {
encode.push_str(&format!("{:02}{:02}{}", i.id, i.info.len(), i.info));
});
}), //TODO
}),
}
encode.push_str(&format!("6304{:04}", self.crc1610));

encode.push_str("6304");
let crc16 = crate::aux::crc16_ccitt(&encode);
encode.push_str(&crc16);
encode
}
}
Expand Down
13 changes: 13 additions & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ fn test_to_string() {
assert_eq!(actual, expected);
}

#[test]
fn assert_both_ways() {
let from = brcode::from_str(&dynamic_code());
let to = brcode::to_string(from);

assert_eq!(to, dynamic_code())
}

#[test]
fn test_brcode_to_string() {
let actual = brcode::brcode_to_string(brcode_expected());
Expand Down Expand Up @@ -77,6 +85,11 @@ fn json_to_brcode_ffi() {
assert_eq!(actual, code);
}

fn dynamic_code() -> String {
"00020101021226740014br.gov.bcb.spi210812345678220412342308123456782420001122334455667788995204000053039865406123.455802BR5913FULANO DE TAL6008BRASILIA62190515RP12345678-201980720014br.gov.bcb.spi2550bx.com.br/spi/U0VHUkVET1RPVEFMTUVOVEVBTEVBVE9SSU8=630434D1"
.to_string()
}

fn code() -> String {
"00020104141234567890123426580014BR.GOV.BCB.PIX0136123e4567-e12b-12d1-a456-42665544000027300012BR.COM.OUTRO011001234567895204000053039865406123.455802BR5917NOME DO RECEBEDOR6008BRASILIA61087007490062190515RP12345678-201980390012BR.COM.OUTRO01190123.ABCD.3456.WXYZ6304AD38"
.to_string()
Expand Down

0 comments on commit ae58407

Please sign in to comment.