Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
d5e2e5fb
R
Rust
项目概览
int
/
Rust
大约 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
d5e2e5fb
编写于
2月 19, 2016
作者:
B
bors
浏览文件
操作
浏览文件
下载
差异文件
Auto merge of #31738 - seanmonstar:sys-rand, r=alexcrichton
上级
15611f75
34dfc399
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
167 addition
and
168 deletion
+167
-168
src/libstd/rand/mod.rs
src/libstd/rand/mod.rs
+93
-2
src/libstd/sys/unix/mod.rs
src/libstd/sys/unix/mod.rs
+1
-0
src/libstd/sys/unix/rand.rs
src/libstd/sys/unix/rand.rs
+0
-166
src/libstd/sys/windows/mod.rs
src/libstd/sys/windows/mod.rs
+1
-0
src/libstd/sys/windows/rand.rs
src/libstd/sys/windows/rand.rs
+72
-0
未找到文件。
src/libstd/rand/mod.rs
浏览文件 @
d5e2e5fb
...
...
@@ -62,6 +62,7 @@
use
io
;
use
mem
;
use
rc
::
Rc
;
use
sys
;
#[cfg(target_pointer_width
=
"32"
)]
use
core_rand
::
IsaacRng
as
IsaacWordRng
;
...
...
@@ -71,9 +72,7 @@
pub
use
core_rand
::{
Rand
,
Rng
,
SeedableRng
};
pub
use
core_rand
::{
XorShiftRng
,
IsaacRng
,
Isaac64Rng
};
pub
use
core_rand
::
reseeding
;
pub
use
rand
::
os
::
OsRng
;
pub
mod
os
;
pub
mod
reader
;
/// The standard RNG. This is designed to be efficient on the current
...
...
@@ -185,3 +184,95 @@ fn fill_bytes(&mut self, bytes: &mut [u8]) {
self
.rng
.borrow_mut
()
.fill_bytes
(
bytes
)
}
}
/// A random number generator that retrieves randomness straight from
/// the operating system. Platform sources:
///
/// - Unix-like systems (Linux, Android, Mac OSX): read directly from
/// `/dev/urandom`, or from `getrandom(2)` system call if available.
/// - Windows: calls `CryptGenRandom`, using the default cryptographic
/// service provider with the `PROV_RSA_FULL` type.
/// - iOS: calls SecRandomCopyBytes as /dev/(u)random is sandboxed.
/// - OpenBSD: uses the `getentropy(2)` system call.
///
/// This does not block.
pub
struct
OsRng
(
sys
::
rand
::
OsRng
);
impl
OsRng
{
/// Create a new `OsRng`.
pub
fn
new
()
->
io
::
Result
<
OsRng
>
{
sys
::
rand
::
OsRng
::
new
()
.map
(
OsRng
)
}
}
impl
Rng
for
OsRng
{
#[inline]
fn
next_u32
(
&
mut
self
)
->
u32
{
self
.0
.next_u32
()
}
#[inline]
fn
next_u64
(
&
mut
self
)
->
u64
{
self
.0
.next_u64
()
}
#[inline]
fn
fill_bytes
(
&
mut
self
,
bytes
:
&
mut
[
u8
])
{
self
.0
.fill_bytes
(
bytes
)
}
}
#[cfg(test)]
mod
tests
{
use
sync
::
mpsc
::
channel
;
use
rand
::
Rng
;
use
super
::
OsRng
;
use
thread
;
#[test]
fn
test_os_rng
()
{
let
mut
r
=
OsRng
::
new
()
.unwrap
();
r
.next_u32
();
r
.next_u64
();
let
mut
v
=
[
0
;
1000
];
r
.fill_bytes
(
&
mut
v
);
}
#[test]
fn
test_os_rng_tasks
()
{
let
mut
txs
=
vec!
();
for
_
in
0
..
20
{
let
(
tx
,
rx
)
=
channel
();
txs
.push
(
tx
);
thread
::
spawn
(
move
||
{
// wait until all the threads are ready to go.
rx
.recv
()
.unwrap
();
// deschedule to attempt to interleave things as much
// as possible (XXX: is this a good test?)
let
mut
r
=
OsRng
::
new
()
.unwrap
();
thread
::
yield_now
();
let
mut
v
=
[
0
;
1000
];
for
_
in
0
..
100
{
r
.next_u32
();
thread
::
yield_now
();
r
.next_u64
();
thread
::
yield_now
();
r
.fill_bytes
(
&
mut
v
);
thread
::
yield_now
();
}
});
}
// start all the threads
for
tx
in
&
txs
{
tx
.send
(())
.unwrap
();
}
}
}
src/libstd/sys/unix/mod.rs
浏览文件 @
d5e2e5fb
...
...
@@ -42,6 +42,7 @@
pub
mod
os_str
;
pub
mod
pipe
;
pub
mod
process
;
pub
mod
rand
;
pub
mod
rwlock
;
pub
mod
stack_overflow
;
pub
mod
thread
;
...
...
src/libstd/
rand/os
.rs
→
src/libstd/
sys/unix/rand
.rs
浏览文件 @
d5e2e5fb
...
...
@@ -8,9 +8,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Interfaces to the operating system provided random number
//! generators.
pub
use
self
::
imp
::
OsRng
;
#[cfg(all(unix,
not(target_os
=
"ios"
),
not(target_os
=
"openbsd"
)))]
...
...
@@ -125,17 +122,6 @@ fn is_getrandom_available() -> bool {
target_arch
=
"powerpc64"
))))]
fn
is_getrandom_available
()
->
bool
{
false
}
/// A random number generator that retrieves randomness straight from
/// the operating system. Platform sources:
///
/// - Unix-like systems (Linux, Android, Mac OSX): read directly from
/// `/dev/urandom`, or from `getrandom(2)` system call if available.
/// - Windows: calls `CryptGenRandom`, using the default cryptographic
/// service provider with the `PROV_RSA_FULL` type.
/// - iOS: calls SecRandomCopyBytes as /dev/(u)random is sandboxed.
/// - OpenBSD: uses the `getentropy(2)` system call.
///
/// This does not block.
pub
struct
OsRng
{
inner
:
OsRngInner
,
}
...
...
@@ -189,17 +175,6 @@ mod imp {
use
sys
::
os
::
errno
;
use
rand
::
Rng
;
/// A random number generator that retrieves randomness straight from
/// the operating system. Platform sources:
///
/// - Unix-like systems (Linux, Android, Mac OSX): read directly from
/// `/dev/urandom`, or from `getrandom(2)` system call if available.
/// - Windows: calls `CryptGenRandom`, using the default cryptographic
/// service provider with the `PROV_RSA_FULL` type.
/// - iOS: calls SecRandomCopyBytes as /dev/(u)random is sandboxed.
/// - OpenBSD: uses the `getentropy(2)` system call.
///
/// This does not block.
pub
struct
OsRng
{
// dummy field to ensure that this struct cannot be constructed outside
// of this module
...
...
@@ -246,17 +221,6 @@ mod imp {
use
rand
::
Rng
;
use
libc
::{
c_int
,
size_t
};
/// A random number generator that retrieves randomness straight from
/// the operating system. Platform sources:
///
/// - Unix-like systems (Linux, Android, Mac OSX): read directly from
/// `/dev/urandom`, or from `getrandom(2)` system call if available.
/// - Windows: calls `CryptGenRandom`, using the default cryptographic
/// service provider with the `PROV_RSA_FULL` type.
/// - iOS: calls SecRandomCopyBytes as /dev/(u)random is sandboxed.
/// - OpenBSD: uses the `getentropy(2)` system call.
///
/// This does not block.
pub
struct
OsRng
{
// dummy field to ensure that this struct cannot be constructed outside
// of this module
...
...
@@ -307,133 +271,3 @@ fn fill_bytes(&mut self, v: &mut [u8]) {
}
}
}
#[cfg(windows)]
mod
imp
{
use
io
;
use
mem
;
use
rand
::
Rng
;
use
sys
::
c
;
/// A random number generator that retrieves randomness straight from
/// the operating system. Platform sources:
///
/// - Unix-like systems (Linux, Android, Mac OSX): read directly from
/// `/dev/urandom`, or from `getrandom(2)` system call if available.
/// - Windows: calls `CryptGenRandom`, using the default cryptographic
/// service provider with the `PROV_RSA_FULL` type.
/// - iOS: calls SecRandomCopyBytes as /dev/(u)random is sandboxed.
/// - OpenBSD: uses the `getentropy(2)` system call.
///
/// This does not block.
pub
struct
OsRng
{
hcryptprov
:
c
::
HCRYPTPROV
}
impl
OsRng
{
/// Create a new `OsRng`.
pub
fn
new
()
->
io
::
Result
<
OsRng
>
{
let
mut
hcp
=
0
;
let
ret
=
unsafe
{
c
::
CryptAcquireContextA
(
&
mut
hcp
,
0
as
c
::
LPCSTR
,
0
as
c
::
LPCSTR
,
c
::
PROV_RSA_FULL
,
c
::
CRYPT_VERIFYCONTEXT
|
c
::
CRYPT_SILENT
)
};
if
ret
==
0
{
Err
(
io
::
Error
::
last_os_error
())
}
else
{
Ok
(
OsRng
{
hcryptprov
:
hcp
})
}
}
}
impl
Rng
for
OsRng
{
fn
next_u32
(
&
mut
self
)
->
u32
{
let
mut
v
=
[
0
;
4
];
self
.fill_bytes
(
&
mut
v
);
unsafe
{
mem
::
transmute
(
v
)
}
}
fn
next_u64
(
&
mut
self
)
->
u64
{
let
mut
v
=
[
0
;
8
];
self
.fill_bytes
(
&
mut
v
);
unsafe
{
mem
::
transmute
(
v
)
}
}
fn
fill_bytes
(
&
mut
self
,
v
:
&
mut
[
u8
])
{
let
ret
=
unsafe
{
c
::
CryptGenRandom
(
self
.hcryptprov
,
v
.len
()
as
c
::
DWORD
,
v
.as_mut_ptr
())
};
if
ret
==
0
{
panic!
(
"couldn't generate random bytes: {}"
,
io
::
Error
::
last_os_error
());
}
}
}
impl
Drop
for
OsRng
{
fn
drop
(
&
mut
self
)
{
let
ret
=
unsafe
{
c
::
CryptReleaseContext
(
self
.hcryptprov
,
0
)
};
if
ret
==
0
{
panic!
(
"couldn't release context: {}"
,
io
::
Error
::
last_os_error
());
}
}
}
}
#[cfg(test)]
mod
tests
{
use
sync
::
mpsc
::
channel
;
use
rand
::
Rng
;
use
super
::
OsRng
;
use
thread
;
#[test]
fn
test_os_rng
()
{
let
mut
r
=
OsRng
::
new
()
.unwrap
();
r
.next_u32
();
r
.next_u64
();
let
mut
v
=
[
0
;
1000
];
r
.fill_bytes
(
&
mut
v
);
}
#[test]
fn
test_os_rng_tasks
()
{
let
mut
txs
=
vec!
();
for
_
in
0
..
20
{
let
(
tx
,
rx
)
=
channel
();
txs
.push
(
tx
);
thread
::
spawn
(
move
||
{
// wait until all the threads are ready to go.
rx
.recv
()
.unwrap
();
// deschedule to attempt to interleave things as much
// as possible (XXX: is this a good test?)
let
mut
r
=
OsRng
::
new
()
.unwrap
();
thread
::
yield_now
();
let
mut
v
=
[
0
;
1000
];
for
_
in
0
..
100
{
r
.next_u32
();
thread
::
yield_now
();
r
.next_u64
();
thread
::
yield_now
();
r
.fill_bytes
(
&
mut
v
);
thread
::
yield_now
();
}
});
}
// start all the threads
for
tx
in
&
txs
{
tx
.send
(())
.unwrap
();
}
}
}
src/libstd/sys/windows/mod.rs
浏览文件 @
d5e2e5fb
...
...
@@ -33,6 +33,7 @@
pub
mod
os_str
;
pub
mod
pipe
;
pub
mod
process
;
pub
mod
rand
;
pub
mod
rwlock
;
pub
mod
stack_overflow
;
pub
mod
thread
;
...
...
src/libstd/sys/windows/rand.rs
0 → 100644
浏览文件 @
d5e2e5fb
// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use
io
;
use
mem
;
use
rand
::
Rng
;
use
sys
::
c
;
pub
struct
OsRng
{
hcryptprov
:
c
::
HCRYPTPROV
}
impl
OsRng
{
/// Create a new `OsRng`.
pub
fn
new
()
->
io
::
Result
<
OsRng
>
{
let
mut
hcp
=
0
;
let
ret
=
unsafe
{
c
::
CryptAcquireContextA
(
&
mut
hcp
,
0
as
c
::
LPCSTR
,
0
as
c
::
LPCSTR
,
c
::
PROV_RSA_FULL
,
c
::
CRYPT_VERIFYCONTEXT
|
c
::
CRYPT_SILENT
)
};
if
ret
==
0
{
Err
(
io
::
Error
::
last_os_error
())
}
else
{
Ok
(
OsRng
{
hcryptprov
:
hcp
})
}
}
}
impl
Rng
for
OsRng
{
fn
next_u32
(
&
mut
self
)
->
u32
{
let
mut
v
=
[
0
;
4
];
self
.fill_bytes
(
&
mut
v
);
unsafe
{
mem
::
transmute
(
v
)
}
}
fn
next_u64
(
&
mut
self
)
->
u64
{
let
mut
v
=
[
0
;
8
];
self
.fill_bytes
(
&
mut
v
);
unsafe
{
mem
::
transmute
(
v
)
}
}
fn
fill_bytes
(
&
mut
self
,
v
:
&
mut
[
u8
])
{
let
ret
=
unsafe
{
c
::
CryptGenRandom
(
self
.hcryptprov
,
v
.len
()
as
c
::
DWORD
,
v
.as_mut_ptr
())
};
if
ret
==
0
{
panic!
(
"couldn't generate random bytes: {}"
,
io
::
Error
::
last_os_error
());
}
}
}
impl
Drop
for
OsRng
{
fn
drop
(
&
mut
self
)
{
let
ret
=
unsafe
{
c
::
CryptReleaseContext
(
self
.hcryptprov
,
0
)
};
if
ret
==
0
{
panic!
(
"couldn't release context: {}"
,
io
::
Error
::
last_os_error
());
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录