Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from ayrat555/master
Update rustler to support OTP 24
- Loading branch information
Showing
11 changed files
with
54 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
elixir 1.12.1-otp-24 | ||
erlang 24.0.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,5 @@ | ||
defmodule ExKeccak do | ||
defdelegate hash_256(data), to: ExKeccak.Impl | ||
use Rustler, otp_app: :ex_keccak, crate: :exkeccak | ||
|
||
def hash_256!(data) do | ||
case hash_256(data) do | ||
{:ok, hash} -> | ||
hash | ||
{:error, :invalid_type} -> | ||
raise ArgumentError, "#{inspect(data)} is not hashable" | ||
end | ||
end | ||
def hash_256(_data), do: :erlang.nif_error(:nif_not_loaded) | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,20 @@ | ||
use rustler::types::binary::{Binary, OwnedBinary}; | ||
use rustler::types::Encoder; | ||
use rustler::{Env, Term}; | ||
use tiny_keccak::{Hasher, Keccak}; | ||
use rustler::types::binary::Binary; | ||
use rustler::types::binary::OwnedBinary; | ||
|
||
mod atoms { | ||
rustler::rustler_atoms! { | ||
atom ok; | ||
atom error; | ||
atom invalid_type; | ||
} | ||
} | ||
|
||
rustler::rustler_export_nifs! { | ||
"Elixir.ExKeccak.Impl", | ||
[ | ||
("hash_256", 1, hash_256, rustler::SchedulerFlags::DirtyCpu) | ||
], | ||
None | ||
} | ||
|
||
fn hash_256<'a>(env: Env<'a>, args: &[Term<'a>]) -> Term<'a> { | ||
let data: Binary = match args[0].decode() { | ||
Ok(binary) => binary, | ||
Err(_error) => return (atoms::error(), atoms::invalid_type()).encode(env), | ||
}; | ||
let data_slice = data.as_slice(); | ||
rustler::init!("Elixir.ExKeccak", [hash_256]); | ||
|
||
#[rustler::nif] | ||
fn hash_256<'a>(data: Binary) -> OwnedBinary { | ||
let mut keccak = Keccak::v256(); | ||
|
||
keccak.update(data_slice); | ||
keccak.update(data.as_slice()); | ||
|
||
let mut result: [u8; 32] = [0; 32]; | ||
keccak.finalize(&mut result); | ||
|
||
let mut erl_bin: OwnedBinary = OwnedBinary::new(32).unwrap(); | ||
erl_bin.as_mut_slice().copy_from_slice(&result); | ||
|
||
(atoms::ok(), erl_bin.release(env)).encode(env) | ||
erl_bin | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters