From 07599ca283feef22be9a640732771a0e8690831c Mon Sep 17 00:00:00 2001 From: goldenMetteyya Date: Sat, 13 Oct 2018 10:04:19 -0400 Subject: [PATCH] rust implementation --- .gitignore | 2 + README.md | 21 ++++- contrib/emojisum-rs/Cargo.lock | 117 ++++++++++++++++++++++++++ contrib/emojisum-rs/Cargo.toml | 13 +++ contrib/emojisum-rs/src/emojimap.json | 1 + contrib/emojisum-rs/src/lib.rs | 99 ++++++++++++++++++++++ 6 files changed, 252 insertions(+), 1 deletion(-) create mode 100644 contrib/emojisum-rs/Cargo.lock create mode 100644 contrib/emojisum-rs/Cargo.toml create mode 120000 contrib/emojisum-rs/src/emojimap.json create mode 100644 contrib/emojisum-rs/src/lib.rs diff --git a/.gitignore b/.gitignore index daf913b..15514dd 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ # Folders _obj _test +target # Architecture specific extensions/prefixes *.[568vq] @@ -22,3 +23,4 @@ _testmain.go *.exe *.test *.prof + diff --git a/README.md b/README.md index a2e4bd7..86a7251 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# emojisum +# Emojisum [![Build Status](https://travis-ci.org/emojisum/emojisum.svg?branch=master)](https://travis-ci.org/emojisum/emojisum) :pray: :paperclip: Emoji that checksum! :tada: :poop: @@ -92,3 +92,22 @@ func main() { } } ``` + +## Contributed Implementations + +### Rust + +In `./contrib/emojisum-rs/` you'll find a rust library for emojisum. + +#### Build + +```shell +cd ./contrib/emojisum-rs/ +cargo build +``` + +#### Test + +```shell +cargo test +``` diff --git a/contrib/emojisum-rs/Cargo.lock b/contrib/emojisum-rs/Cargo.lock new file mode 100644 index 0000000..4a69e96 --- /dev/null +++ b/contrib/emojisum-rs/Cargo.lock @@ -0,0 +1,117 @@ +[[package]] +name = "arrayvec" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "nodrop 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "blake2-rfc" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "arrayvec 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", + "constant_time_eq 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "constant_time_eq" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "emojisum" +version = "0.0.1" +dependencies = [ + "blake2-rfc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.79 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.79 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.32 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "itoa" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "nodrop" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "proc-macro2" +version = "0.4.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "quote" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 0.4.20 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "ryu" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "serde" +version = "1.0.79" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "serde_derive" +version = "1.0.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 0.4.20 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde_json" +version = "1.0.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", + "ryu 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.79 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "syn" +version = "0.15.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 0.4.20 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "unicode-xid" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[metadata] +"checksum arrayvec 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)" = "a1e964f9e24d588183fcb43503abda40d288c8657dfc27311516ce2f05675aef" +"checksum blake2-rfc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "5d6d530bdd2d52966a6d03b7a964add7ae1a288d25214066fd4b600f0f796400" +"checksum constant_time_eq 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8ff012e225ce166d4422e0e78419d901719760f62ae2b7969ca6b564d1b54a9e" +"checksum itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1306f3464951f30e30d12373d31c79fbd52d236e5e896fd92f96ec7babbbe60b" +"checksum nodrop 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "9a2228dca57108069a5262f2ed8bd2e82496d2e074a06d1ccc7ce1687b6ae0a2" +"checksum proc-macro2 0.4.20 (registry+https://github.com/rust-lang/crates.io-index)" = "3d7b7eaaa90b4a90a932a9ea6666c95a389e424eff347f0f793979289429feee" +"checksum quote 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)" = "dd636425967c33af890042c483632d33fa7a18f19ad1d7ea72e8998c6ef8dea5" +"checksum ryu 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7153dd96dade874ab973e098cb62fcdbb89a03682e46b144fd09550998d4a4a7" +"checksum serde 1.0.79 (registry+https://github.com/rust-lang/crates.io-index)" = "84257ccd054dc351472528c8587b4de2dbf0dc0fe2e634030c1a90bfdacebaa9" +"checksum serde_derive 1.0.79 (registry+https://github.com/rust-lang/crates.io-index)" = "31569d901045afbff7a9479f793177fe9259819aff10ab4f89ef69bbc5f567fe" +"checksum serde_json 1.0.32 (registry+https://github.com/rust-lang/crates.io-index)" = "43344e7ce05d0d8280c5940cabb4964bea626aa58b1ec0e8c73fa2a8512a38ce" +"checksum syn 0.15.9 (registry+https://github.com/rust-lang/crates.io-index)" = "b10ee269228fb723234fce98e9aac0eaed2bd5f1ad2f6930e8d5b93f04445a1a" +"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" diff --git a/contrib/emojisum-rs/Cargo.toml b/contrib/emojisum-rs/Cargo.toml new file mode 100644 index 0000000..3daa05a --- /dev/null +++ b/contrib/emojisum-rs/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "emojisum" +version = "0.0.1" +authors = ["hello@organism.ai"] +description = "Emoji based cheksums" +license = "Apache-2.0" +repository = "https://github.com/stichtingorganism/emojisum" + +[dependencies] +blake2-rfc = "0.2" +serde = "1.0" +serde_json = "1.0" +serde_derive = "1.0" \ No newline at end of file diff --git a/contrib/emojisum-rs/src/emojimap.json b/contrib/emojisum-rs/src/emojimap.json new file mode 120000 index 0000000..1de0c09 --- /dev/null +++ b/contrib/emojisum-rs/src/emojimap.json @@ -0,0 +1 @@ +../../../emoji/emojimap.json \ No newline at end of file diff --git a/contrib/emojisum-rs/src/lib.rs b/contrib/emojisum-rs/src/lib.rs new file mode 100644 index 0000000..9f1beac --- /dev/null +++ b/contrib/emojisum-rs/src/lib.rs @@ -0,0 +1,99 @@ +// Copyright 2018 Stichting Organism +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Emojisum + + + +//External Crates +extern crate blake2_rfc; +extern crate serde; +extern crate serde_json; +#[macro_use] extern crate serde_derive; + +//Imports +use std::fs::File; +use std::path::Path; +use blake2_rfc::blake2b::{blake2b}; + + + + +//Holds main info parsed from standard +#[derive(Deserialize, Debug)] +pub struct Emojisum { + version: String, + description: String, + // these are an ordered list, referened by a byte (each byte of a checksum digest) + emojiwords: Vec> +} + +// Words are a set of options to represent an emoji. +// Possible options could be the ":colon_notation:" or a "U+26CF" style codepoint. +//pub type Word = String; + + + +impl Emojisum { + + //Pass a emojimapping JSON to start + pub fn init(file_path: &str) -> Emojisum { + let json_file_path = Path::new(file_path); + let json_file = File::open(json_file_path).expect("file not found"); + let deserialized: Emojisum = + serde_json::from_reader(json_file).expect("error while reading json"); + + return deserialized; + } + + //given a series of bytes match emojis + pub fn from_bytes(&self, tosum: &[u8]) -> Option { + //check that it is 32bytes + if tosum.len() < 32 { return None } + + let mut result = String::new(); + + for byte in tosum { + result.push_str(&(self.emojiwords[*byte as usize])[0]) + } + + return Some(result); + + } + + //given a vector of bytes, we hash and return checksum + pub fn hash_to_emojisum(&self, data: Vec) -> Option { + //get 256bit hash of given data + let hash = blake2b(32, &[], &data); + return self.from_bytes(hash.as_bytes()) + } +} + + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + + let e = Emojisum::init("./src/emojimap.json"); + //println!("{:?}", e); + + println!("{:?}", e.hash_to_emojisum("dog".as_bytes().to_vec())); + println!("{:?}", e.hash_to_emojisum("cat".as_bytes().to_vec())); + + assert_eq!(2 + 2, 4); + } +}