Created
October 14, 2019 22:46
-
-
Save naranjja/6a85c88f79383fc5e67a4fbe46d88dc1 to your computer and use it in GitHub Desktop.
Hash minute timestamp + userId on Node.js, Nativescript, and R
This file contains hidden or 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
// 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); |
This file contains hidden or 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
// 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); |
This file contains hidden or 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
# 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