wrapper: add a helper to generate numbers from a CSPRNG
[git.git] / t / helper / test-csprng.c
blob65d14973c520ea739543080d6d6fe8c1641d9f34
1 #include "test-tool.h"
2 #include "git-compat-util.h"
5 int cmd__csprng(int argc, const char **argv)
7 unsigned long count;
8 unsigned char buf[1024];
10 if (argc > 2) {
11 fprintf(stderr, "usage: %s [<size>]\n", argv[0]);
12 return 2;
15 count = (argc == 2) ? strtoul(argv[1], NULL, 0) : -1L;
17 while (count) {
18 unsigned long chunk = count < sizeof(buf) ? count : sizeof(buf);
19 if (csprng_bytes(buf, chunk) < 0) {
20 perror("failed to read");
21 return 5;
23 if (fwrite(buf, chunk, 1, stdout) != chunk)
24 return 1;
25 count -= chunk;
28 return 0;