Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dup-crypto-rs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
libs
dup-crypto-rs
Commits
73ab1639
Commit
73ab1639
authored
5 years ago
by
Éloïs
Browse files
Options
Downloads
Patches
Plain Diff
[feat] rand: generating an arbitrary number of random bytes
parent
a42ea889
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!3
Elois/rand
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/hashs/mod.rs
+3
-4
3 additions, 4 deletions
src/hashs/mod.rs
src/rand.rs
+56
-5
56 additions, 5 deletions
src/rand.rs
src/seeds.rs
+2
-4
2 additions, 4 deletions
src/seeds.rs
with
61 additions
and
13 deletions
src/hashs/mod.rs
+
3
−
4
View file @
73ab1639
...
...
@@ -17,7 +17,7 @@
use
crate
::
bases
::
*
;
use
crate
::
rand
::
UnspecifiedRandError
;
use
ring
::
{
digest
,
rand
}
;
use
ring
::
digest
;
#[cfg(feature
=
"ser"
)]
use
serde
::{
Deserialize
,
Serialize
};
use
std
::
fmt
::{
Debug
,
Display
,
Error
,
Formatter
};
...
...
@@ -60,9 +60,8 @@ impl Hash {
/// Generate a random Hash
#[inline]
pub
fn
random
()
->
Result
<
Self
,
UnspecifiedRandError
>
{
let
random_bytes
=
rand
::
generate
::
<
[
u8
;
32
]
>
(
&
rand
::
SystemRandom
::
new
())
.map_err
(|
_
|
UnspecifiedRandError
)
?
;
Ok
(
Hash
(
random_bytes
.expose
()))
let
random_bytes
=
crate
::
rand
::
gen_32_bytes
()
.map_err
(|
_
|
UnspecifiedRandError
)
?
;
Ok
(
Hash
(
random_bytes
))
}
/// Compute hash of any binary datas
...
...
This diff is collapsed.
Click to expand it.
src/rand.rs
+
56
−
5
View file @
73ab1639
...
...
@@ -16,7 +16,7 @@
//! Manage random generation.
use
byteorder
::
ByteOrder
;
use
ring
::
rand
;
use
ring
::
rand
::{
generate
,
SystemRandom
}
;
/// An error with absolutely no details.
///
...
...
@@ -37,21 +37,72 @@ use ring::rand;
#[derive(Copy,
Clone,
Debug,
PartialEq)]
pub
struct
UnspecifiedRandError
;
/// Secure random bytes generator
pub
fn
gen_random_bytes
(
buffer
:
&
mut
[
u8
])
->
Result
<
(),
UnspecifiedRandError
>
{
let
mut
cursor
=
0
;
let
mut
remaining_len
=
buffer
.len
();
while
remaining_len
>=
32
{
buffer
[
cursor
..
(
cursor
+
32
)]
.copy_from_slice
(
&
gen_32_bytes
()
?
[
..
]);
cursor
+=
32
;
remaining_len
-=
32
;
}
while
remaining_len
>=
16
{
buffer
[
cursor
..
(
cursor
+
16
)]
.copy_from_slice
(
&
gen_16_bytes
()
?
[
..
]);
cursor
+=
16
;
remaining_len
-=
16
;
}
if
remaining_len
>
0
{
buffer
[
cursor
..
]
.copy_from_slice
(
&
gen_16_bytes
()
?
[
..
remaining_len
]);
}
Ok
(())
}
#[inline]
/// Generate random u32
pub
fn
gen_u32
()
->
Result
<
u32
,
UnspecifiedRandError
>
{
let
rng
=
rand
::
SystemRandom
::
new
();
let
random_bytes
=
rand
::
generate
::
<
[
u8
;
4
]
>
(
&
rng
)
.map_err
(|
_
|
UnspecifiedRandError
)
?
;
let
random_bytes
=
generate
::
<
[
u8
;
4
]
>
(
&
SystemRandom
::
new
())
.map_err
(|
_
|
UnspecifiedRandError
)
?
;
Ok
(
byteorder
::
BigEndian
::
read_u32
(
&
random_bytes
.expose
()))
}
#[inline]
/// Generate random 16 bytes
pub
fn
gen_16_bytes
()
->
Result
<
[
u8
;
16
],
UnspecifiedRandError
>
{
let
random_bytes
=
generate
::
<
[
u8
;
16
]
>
(
&
SystemRandom
::
new
())
.map_err
(|
_
|
UnspecifiedRandError
)
?
;
Ok
(
random_bytes
.expose
())
}
#[inline]
/// Generate random 32 bytes
pub
fn
gen_32_bytes
()
->
Result
<
[
u8
;
32
],
UnspecifiedRandError
>
{
let
random_bytes
=
generate
::
<
[
u8
;
32
]
>
(
&
SystemRandom
::
new
())
.map_err
(|
_
|
UnspecifiedRandError
)
?
;
Ok
(
random_bytes
.expose
())
}
#[cfg(test)]
mod
tests
{
use
super
::
*
;
#[test]
fn
test_gen_u32
()
{
assert_ne!
(
gen_u32
(),
gen_u32
())
fn
test_gen_u32
()
->
Result
<
(),
UnspecifiedRandError
>
{
assert_ne!
(
gen_u32
()
?
,
gen_u32
()
?
);
Ok
(())
}
#[test]
fn
test_gen_random_bytes
()
->
Result
<
(),
UnspecifiedRandError
>
{
let
mut
buffer
=
[
0u8
;
51
];
gen_random_bytes
(
buffer
.as_mut
())
?
;
let
mut
buffer
=
[
0u8
;
48
];
gen_random_bytes
(
buffer
.as_mut
())
?
;
Ok
(())
}
}
This diff is collapsed.
Click to expand it.
src/seeds.rs
+
2
−
4
View file @
73ab1639
...
...
@@ -18,7 +18,6 @@
use
crate
::
bases
::
b58
::{
bytes_to_str_base58
,
ToBase58
};
use
crate
::
bases
::
*
;
use
crate
::
rand
::
UnspecifiedRandError
;
use
ring
::
rand
;
#[cfg(feature
=
"ser"
)]
use
serde
::{
Deserialize
,
Serialize
};
use
std
::
fmt
::{
self
,
Debug
,
Display
,
Formatter
};
...
...
@@ -68,9 +67,8 @@ impl Seed32 {
#[inline]
/// Generate random seed
pub
fn
random
()
->
Result
<
Seed32
,
UnspecifiedRandError
>
{
let
random_bytes
=
rand
::
generate
::
<
[
u8
;
32
]
>
(
&
rand
::
SystemRandom
::
new
())
.map_err
(|
_
|
UnspecifiedRandError
)
?
;
Ok
(
Seed32
::
new
(
random_bytes
.expose
()))
let
random_bytes
=
crate
::
rand
::
gen_32_bytes
()
.map_err
(|
_
|
UnspecifiedRandError
)
?
;
Ok
(
Seed32
::
new
(
random_bytes
))
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment