Last active
August 29, 2015 14:12
-
-
Save jmcastagnetto/2253d846af1c60f5c0da to your computer and use it in GitHub Desktop.
A simple and naive example of using the NIST Randomness Beacon in R (https://beacon.nist.gov/home)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require(RCurl) | |
require(XML) | |
# Let's make a special class | |
NISTBeaconResponse <- function (ts) { | |
if(!is.integer(ts) & | |
!inherits(ts, "POSIXct") & | |
!inherits(ts, "POSIXlt")) { | |
stop("We expected a unix timestamp as an integer or a POSIXct or POSIXlt value") | |
} | |
tsval <- ifelse(inherits(ts, "POSIXct") | inherits(ts, "POSIXlt"), | |
as.integer(ts), ts) | |
url <- paste0("https://beacon.nist.gov/rest/record/", tsval) | |
headers <- basicTextGatherer() # might be a good idea to use this to check the HTTP status code | |
response <- getURL(url, headerfunction=headers$update) | |
# use the following line instead, if you get cert validation errors | |
# response <- getURL(url, .opts = list(ssl.verifypeer = FALSE), headerfunction=headers$update) | |
beacon <- structure(as.data.frame(xmlToList(xmlParse(response, asText=TRUE))), | |
class="NISTBeaconResponse") | |
beacon$frequency <- as.integer(beacon$frequency) | |
beacon$timeStamp <- as.integer(beacon$timeStamp) | |
beacon$statusCode <- as.integer(beacon$statusCode) | |
beacon | |
} | |
# and a way to print it | |
print.NISTBeaconResponse <- function(response) { | |
if(!inherits(response, "NISTBeaconResponse")) { | |
stop("An object of type NISTBeaconResponse was expected") | |
} | |
breakline <- function(longline) { | |
lines <- gsub('(.{1,50})', '\\1\n', longline) | |
lines <- gsub('\n', '\n\t\t\t', lines, fixed=TRUE) | |
lines | |
} | |
output <- "\tNIST Randomness Beacon Response\n\t===============================\n\n" | |
output <- paste0(output, "\t* version:\t", response$version, "\n") | |
output <- paste0(output, "\t* frequency:\t", response$frequency, " seconds\n") | |
output <- paste0(output, "\t* seed:\t\t", breakline(response$seedValue), "\n") | |
output <- paste0(output, "\t* status:\t", response$statusCode, "\n") | |
output <- paste0(output, "\t* timestamp:\t", as.POSIXct(response$timeStamp, origin="1970-01-01"), | |
" (", response$timeStamp,")\n") | |
output <- paste0(output, "\t* output:\t", breakline(response$outputValue), "\n") | |
output <- paste0(output, "\t* prev value:\t", breakline(response$previousOutputValue), "\n") | |
output <- paste0(output, "\t* signature:\t", breakline(response$signatureValue), "\n") | |
cat(output) | |
} | |
# test the code | |
ts <- Sys.time() | |
beacon <- NISTBeaconResponse(ts) | |
print(beacon) |
Here's the link to that site's certificate:
https://beacon.nist.gov/certificate/beacon.cer
It could probably be explicitly added to the function somehow.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For those that for unknown reasons do not have the local certs needed to validate the HTTPS connection to NIST, try replacing line #15 above with:
Note: Updated the gist with a comment to include this modification