]> Repositorios git - scryer-prolog.git/commitdiff
ADDED: ripemd160 digest algorithm
authorMarkus Triska <[email protected]>
Wed, 13 May 2020 18:36:25 +0000 (20:36 +0200)
committerMarkus Triska <[email protected]>
Wed, 13 May 2020 20:36:40 +0000 (22:36 +0200)
This is used for example for Bitcoin address generation.

Cargo.toml
src/prolog/lib/crypto.pl
src/prolog/machine/system_calls.rs

index 8e63ec9a8c2d44be90f1db3de56ad2327beeca83..9f9dc993dde97dd3f6c4875dcf9f4581e5a96e60 100644 (file)
@@ -35,3 +35,4 @@ rug = { version = "1.4.0", optional = true }
 rustyline = "6.0.0"
 unicode_reader = "1.0.0"
 ring = "0.16.13"
+ripemd160 = "0.8.0"
index 67b5ac80f8f0fb27cc558cbdc6350d8a6b5ed060..44e8547e84d0ffd13642edda3049191a1326152b 100644 (file)
@@ -138,7 +138,8 @@ crypto_random_byte(B) :- '$crypto_random_byte'(B).
 
       algorithm(A)
 
-   where A is one of sha256, sha384, sha512, sha512_256, or a variable.
+   where A is one of ripemd160, sha256, sha384, sha512, sha512_256,
+   or a variable.
 
    If A is a variable, then it is unified with the default algorithm,
    which is an algorithm that is considered cryptographically secure
@@ -167,6 +168,7 @@ crypto_data_hash(Data, Hash, Options) :-
         '$crypto_data_hash'(Data, HashBytes, A),
         hex_bytes(Hash, HashBytes).
 
+hash_algorithm(ripemd160).
 hash_algorithm(sha256).
 hash_algorithm(sha512).
 hash_algorithm(sha384).
index 47d7f91104bb3a00db7c28cee0ecca0504691085..b572af41ef8ad054f2b757538c003aeba66bfa9d 100644 (file)
@@ -40,6 +40,7 @@ use crate::crossterm::terminal::{enable_raw_mode, disable_raw_mode};
 
 use ring::rand::{SecureRandom, SystemRandom};
 use ring::digest;
+use ripemd160::{Ripemd160, Digest};
 
 pub fn get_key() -> KeyEvent {
     let key;
@@ -5255,18 +5256,23 @@ impl MachineState {
                     }
                 };
 
-                let hash = digest::digest(
-                              match algorithm_str {
-                              "sha256" =>     { &digest::SHA256 }
-                              "sha384" =>     { &digest::SHA384 }
-                              "sha512" =>     { &digest::SHA512 }
-                              "sha512_256" => { &digest::SHA512_256 }
-                              _ =>            { unreachable!() }
-                              },
-                              &bytes);
-
-                let ints = hash.as_ref().iter().map(|b| HeapCellValue::Integer(Rc::new(Integer::from(*b))));
-                let ints_list = Addr::HeapCell(self.heap.to_list(ints));
+                let ints_list =
+                        if algorithm_str == "ripemd160" {
+                             let mut context = Ripemd160::new();
+                             context.input(&bytes);
+                             Addr::HeapCell(self.heap.to_list(context.result().as_ref().iter().map(|b| HeapCellValue::Integer(Rc::new(Integer::from(*b))))))
+                        } else {
+                             let ints = digest::digest(
+                                            match algorithm_str {
+                                               "sha256" =>     { &digest::SHA256 }
+                                               "sha384" =>     { &digest::SHA384 }
+                                               "sha512" =>     { &digest::SHA512 }
+                                               "sha512_256" => { &digest::SHA512_256 }
+                                               _ =>            { unreachable!() }
+                                            },
+                                            &bytes);
+                             Addr::HeapCell(self.heap.to_list(ints.as_ref().iter().map(|b| HeapCellValue::Integer(Rc::new(Integer::from(*b))))))
+                        };
 
                 self.unify(self[temp_v!(2)], ints_list);
             }