1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//! vivacity-core: manifests, platform, fetch and installation.
pub mod binproxy;
pub mod clone;
pub mod constraint;
pub mod content_hash;
pub mod dirs;
pub mod error;
pub mod extract;
pub mod fetch;
pub mod glob;
pub mod installer;
pub mod installers;
pub mod jsonfile;
pub mod layout;
pub mod lock;
pub mod path_install;
pub mod pathutil;
pub mod pest_plugin;
pub mod phparray;
pub mod phpcs_installer;
pub mod phpjson;
pub mod phpserialize;
pub mod platform;
pub mod root_version;
pub mod runtime_stub;
pub mod scope;
pub mod state;
pub mod store;
pub mod version;
pub use error::{Error, Result};
/// `random_bytes($n)` for the few places Composer draws randomness (the
/// APCu prefix): the OS entropy source, or the hasher seed as a fallback.
pub fn random_bytes(n: usize) -> Vec<u8> {
let mut out = vec![0u8; n];
#[cfg(unix)]
{
use std::io::Read as _;
if let Ok(mut f) = std::fs::File::open("/dev/urandom") {
if f.read_exact(&mut out).is_ok() {
return out;
}
}
}
use std::hash::{BuildHasher as _, Hasher as _};
let mut i = 0;
while i < n {
let word = std::collections::hash_map::RandomState::new()
.build_hasher()
.finish();
for b in word.to_le_bytes() {
if i < n {
out[i] = b;
i += 1;
}
}
}
out
}