Skip to content
Permalink
Browse files

rust implementation

  • Loading branch information...
goldenMetteyya authored and vbatts committed Oct 13, 2018
1 parent 31734a1 commit 07599ca283feef22be9a640732771a0e8690831c
@@ -6,6 +6,7 @@
# Folders
_obj
_test
target

# Architecture specific extensions/prefixes
*.[568vq]
@@ -22,3 +23,4 @@ _testmain.go
*.exe
*.test
*.prof

@@ -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
```

Some generated files are not rendered by default. Learn more.

@@ -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"
@@ -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<Vec<String>>
}

// 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<String> {
//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<u8>) -> Option<String> {
//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);
}
}

0 comments on commit 07599ca

Please sign in to comment.
You can’t perform that action at this time.