// Save the contents of the given FileContents object under the given // filename. Return 0 on success. int SaveFileContents(const char* filename, FileContents file) { int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, S_IRUSR | S_IWUSR); if (fd < 0) { printf("failed to open \"%s\" for write: %s\n", filename, strerror(errno)); return -1; } ssize_t bytes_written = FileSink(file.data, file.size, &fd); if (bytes_written != file.size) { printf("short write of \"%s\" (%ld bytes of %ld) (%s)\n", filename, (long)bytes_written, (long)file.size, strerror(errno)); close(fd); return -1; } fsync(fd); close(fd); if (chmod(filename, file.st.st_mode) != 0) { printf("chmod of \"%s\" failed: %s\n", filename, strerror(errno)); return -1; } if (chown(filename, file.st.st_uid, file.st.st_gid) != 0) { printf("chown of \"%s\" failed: %s\n", filename, strerror(errno)); return -1; } return 0; }
void FIPS140_GenerateRandomFiles() { #ifdef OS_RNG_AVAILABLE AutoSeededX917RNG<DES_EDE3> rng; RandomNumberStore store(rng, ULONG_MAX); for (unsigned int i=0; i<100000; i++) store.TransferTo(FileSink((IntToString(i) + ".rnd").c_str()).Ref(), 20000); #else cout << "OS provided RNG not available.\n"; exit(-1); #endif }