Skip to content

Instantly share code, notes, and snippets.

@naranjja
Created October 14, 2019 22:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naranjja/6a85c88f79383fc5e67a4fbe46d88dc1 to your computer and use it in GitHub Desktop.
Save naranjja/6a85c88f79383fc5e67a4fbe46d88dc1 to your computer and use it in GitHub Desktop.
Hash minute timestamp + userId on Node.js, Nativescript, and R
// import createHash function from crypto (default Node.js library)
const { createHash } = require("crypto");
// get current date in UTC as ISO string and keep all chars up until minutes
// eg. 2019-10-14T17:40
const date = new Date().toISOString().slice(0, 16);
// get userId
const userId = "0000123";
// join both date and userId with no space in between
const hashable = [ date, userId ].join("");
// create a MD5 hash and digest it to hex
const hash = createHash("md5").update(hashable).digest("hex");
console.log(hash);
// import MD5 function from nativescript-md5 (https://www.npmjs.com/package/nativescript-md5)
const { MD5 } = require("nativescript-md5");
// get current date in UTC as ISO string and keep all chars up until minutes
// eg. 2019-10-14T17:40
const date = new Date().toISOString().slice(0, 16);
// get userId
const userId = "0000123";
// join both date and userId with no space in between
const hashable = [ date, userId ].join("");
// create a MD5 hash and digest it to hex
const hash = MD5.hashForString(hashable);
console.log(hash);
# import digest library (default R-base library)
library(digest)
# get current date in UTC as ISO string and keep all chars up until minutes
# eg. 2019-10-14T17:40
date <- strftime(as.POSIXlt(Sys.time(), tz = "UTC"), "%Y-%m-%dT%H:%M")
# get userId
userId <- "0000123"
# join both date and userId with no space in between
hashable <- paste(date, userId, sep = "")
# create a MD5 hash and digest it to hex
hash <- digest(hashable, algo="md5", ascii = TRUE, raw = FALSE, serialize = FALSE)
print(hash)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment