I need to get my google one-time password every time when I receive a new one.
Please check here also.
I want to use this code inside my app.js (in server side java script file). I have been trying to figure it out but could't make it.
I copied and pasted to all code in that website and created at under same directory and required it.
I tried this:
myKey = '**my_key_is_here';
require('./sha.js');
function dec2hex(s) { return (s < 15.5 ? '0' : '') + Math.round(s).toString(16); }
function hex2dec(s) { return parseInt(s, 16); }
function base32tohex(base32) {
var base32chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
var bits = "";
var hex = "";
for (var i = 0; i < base32.length; i++) {
var val = base32chars.indexOf(base32.charAt(i).toUpperCase());
bits += leftpad(val.toString(2), 5, '0');
}
for (var i = 0; i + 4 <= bits.length; i += 4) {
var chunk = bits.substr(i, 4);
hex = hex + parseInt(chunk, 2).toString(16);
}
return hex;
}
function leftpad(str, len, pad) {
if (len + 1 >= str.length) {
str = Array(len + 1 - str.length).join(pad) + str;
}
return str;
}
function updateOtp() {
var key = base32tohex(myKey);
var epoch = Math.round(new Date().getTime() / 1000.0);
var time = leftpad(dec2hex(Math.floor(epoch / 30)), 16, '0');
// updated for jsSHA v2.0.0 - http://caligatio.github.io/jsSHA/
var shaObj = new jsSHA("SHA-1", "HEX");
shaObj.setHMACKey(key, "HEX");
shaObj.update(time);
var hmac = shaObj.getHMAC("HEX");
if (hmac == 'KEY MUST BE IN BYTE INCREMENTS') {
console.log('something wrong with HMAC');
} else {
var offset = hex2dec(hmac.substring(hmac.length - 1));
var part1 = hmac.substr(0, offset * 2);
var part2 = hmac.substr(offset * 2, 8);
var part3 = hmac.substr(offset * 2 + 8, hmac.length - offset);
}
var otp = (hex2dec(hmac.substr(offset * 2, 8)) & hex2dec('7fffffff')) + '';
otp = (otp).substr(otp.length - 6, 6);
var test = otp;
console.log(test);
}
function timer() {
var epoch = Math.round(new Date().getTime() / 1000.0);
var countDown = 30 - (epoch % 30);
if (epoch % 30 == 0) updateOtp();
// $('#updatingIn').text(countDown);
}
function startFactor() {
updateOtp();
setInterval(timer, 1000);
};
startFactor();
but getting this output :
ReferenceError: jsSHA is not defined
Basic Question is: How can I use this file in my Nodejs project.
Install it from npm repo:
npm install jssha --save or npm install jssha --save-dev
and then require:
jsSHA = require("jssha");
It was so simple to do with speakeasy !
That was what I needed.
Solved.
Related
for (let i = 1; i < data.values.length; i++) {
TimeStart = data.values[i].time;
HightStart = data.values[i].value;
TimeEnd = data.values[i].time;
HightEnd = data.values[i].value;
if (TimeStart.slice(11, 16) == start) {
Date = TimeStart.slice(0, 10);
TimeStart = TimeStart.slice(11, 16);
tidedata1.innerHTML = `${TimeStart} - ${HightStart}m`;
tidedata1Full.innerHTML = `${TimeStart}am - ${HightStart}m`;
}
if (TimeEnd.slice(11, 16) == end) {
Date = TimeEnd.slice(0, 10);
TimeEnd = TimeEnd.slice(11, 16);
tidedata2.innerHTML = `${TimeEnd} - ${HightEnd}m`;
tidedata2Full.innerHTML = `${TimeEnd}pm - ${HightEnd}m`;
}
}
if (i < day) {
square.classList.add("disabled");
}
square2.append(tidedata1Full, tidedata2Full);
square.append(monthNum, tidedata1, tidedata2, square2);
inner_grid.append(square);
}
});
calendar.append(month, inner_grid);
At the moment it will get the correct data but only for one day and then it just repeats that same data over all of the days.
the data structure look like
Using a for-each loop to go over every point of data in the array.
And then append the data to where it needs to go. In this case to the calendar and its divs.
lowHigh.forEach(d =>{
let wDate = new Date(d.time).getUTCDate();
let wHour = 6;
let eHour = 18;
wHour = ("0" + wHour).slice(-2);
let wMintue = new Date(d.time).getUTCMinutes();
wMintue = ("0" + wHour).slice(-2);
if(wDate == i+1)
{
if(tidedata1.innerHTML == "")
{
tidedata1.innerHTML = `${wHour}:${wMintue} - ${d.value}m`
tidedata1Full.innerHTML = `${wHour}:${wMintue}am - ${d.value}m`
}
else
{
tidedata2.innerHTML = `${eHour}:${wMintue} - ${d.value}m`
tidedata2Full.innerHTML = `${eHour}:${wMintue}pm - ${d.value}m`
}
}
})
if(i < day)
{
square.classList.add("disabled");
}
square2.append(tidedata1Full, tidedata2Full)
square.append(monthNum,tidedata1,tidedata2, square2);
inner_grid.append(square);
}
})
My Code:
const crypto = require('crypto');
const crashHash = '';
// Hash from bitcoin block #610546. Public seed event: https://twitter.com/Roobet/status/1211800855223123968
const salt = '0000000000000000000fa3b65e43e4240d71762a5bf397d5304b2596d116859c';
function saltHash(hash) {
return crypto
.createHmac('sha256', hash)
.update(salt)
.digest('hex');
}
function generateHash(seed) {
return crypto
.createHash('sha256')
.update(seed)
.digest('hex');
}
function divisible(hash, mod) {
// We will read in 4 hex at a time, but the first chunk might be a bit smaller
// So ABCDEFGHIJ should be chunked like AB CDEF GHIJ
var val = 0;
var o = hash.length % 4;
for (var i = o > 0 ? o - 4 : 0; i < hash.length; i += 4) {
val = ((val << 16) + parseInt(hash.substring(i, i + 4), 16)) % mod;
}
return val === 0;
}
function crashPointFromHash(serverSeed) {
const hash = crypto
.createHmac('sha256', serverSeed)
.update(salt)
.digest('hex');
const hs = parseInt(100 / 4);
if (divisible(hash, hs)) {
return 1;
}
const h = parseInt(hash.slice(0, 52 / 4), 16);
const e = Math.pow(2, 52);
return Math.floor((100 * e - h) / (e - h)) / 100.0;
}
function getPreviousGames() {
const previousGames = [];
let gameHash = generateHash(crashHash);
for (let i = 0; i < 100; i++) {
const gameResult = crashPointFromHash(gameHash);
previousGames.push({ gameHash, gameResult });
gameHash = generateHash(gameHash);
}
return previousGames;
}
function verifyCrash() {
const gameResult = crashPointFromHash(crashHash);
const previousHundredGames = getPreviousGames();
return { gameResult, previousHundredGames };
}
console.log(verifyCrash());
Code Sandbox
I'm trying to make this code show the results it already shows, but I want it to add something to the end of each gameResult data so it would look like this: gameResult: 4.39 "maybe"
I've tried to add something like this to the code with no luck. I had it working to the point where it would only return the very first gameResult but not the ones after. If someone could help that would be great, or if you have another way other than this code below that I was trying to use, that works too.
function gameResult
const result =
if (gameResult === 1) {
return "no";
};
if (gameResult <= 3) {
return "maybe";
};
if (gameResult <= 10) {
return "yes";
};
So, if I understand correctly the expected output should be like this,
{
"gameResult": "4.39 "yes"",
"previousHundredGames": [...]
}
I am able to do this by modifying the verifyCrash function to this,
function verifyCrash() {
let gameResult = crashPointFromHash(crashHash);
const previousHundredGames = getPreviousGames();
if (gameResult === 1) {
gameResult=+' "no"';
}
if (gameResult <= 3) {
gameResult=+' "maybe"';
}
if (gameResult <= 10) {
gameResult+= ' "yes"';
}
return { gameResult, previousHundredGames };
}
Check this link to see it in action,
https://codesandbox.io/s/crash-forked-f7fb7
Hello i am having some troubles with my code i am fairly new to this if anyone could assist me Thanks.
var a = 5;
var b = 6;
var c = 7;
document.write(Math.floor((Math.random() * 3) + 5));
if (5) {
alert("mission failed we will get them next time");
} else {
alert("sorry");
}
Please start with learning documentation => https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else
let RandomVal = Math.floor((Math.random() * 3) + 5);
console.log(RandomVal);
if (RandomVal===5) {
console.log("mission failed we will get them next time");
} else {
console.log("sorry");
}
I want to generate an Unique 5 digits ID + 784 at the begining, the constraint, I can execute the script only one time, and I have to avoid the first 100 numbers so It can't be 00100 and lower. Since I use timestamp and I can execute only my script one time how I can handle this ?
I did this it's maybe dumb but at least I tried.
ConcatedID();
function ConcatedID()
{
var uniqID = checkProtectedRange();
if (checkProtectedRange())
{
var BarcodeID = 784 + uniqID;
return BarcodeID;
}
else
checkProtectedRange();
}
function checkProtectedRange()
{
var uniqueID = GenerateUniqueID();
var checkRange = uniqueID.substr(uniqueID.length - 3);
var checkRangeINT = parseInt(checkRange);
if (checkRangeINT <= 100)
return (false);
else
return (true);
}
function GenerateUniqueID()
{
var lengthID = 5;
var timestamp = + new Date();
var ts = timestamp.toString();
var parts = ts.split("").reverse();
var id = "";
var min = 0;
var max = parts.length -1;
for (var i = 0; i < lengthID; ++i)
{
var index = Math.floor(Math.random() * (max - min + 1)) + min;
id += parts[index];
}
gs.log('Generate ID ' + id);
return id;
}
Without being able to track previously used IDs, you're left with chance to prevent duplicates. Your shenanigans with Date doesn't really change that. See the birthday problem.
Given that, just follow the most straight-forward method: Generate a random string consisting of five digits.
function GenerateUniqueID() {
return ('0000'+(Math.random() * (100000 - 101) + 101)|0).slice(-5);
}
Or, if you want just the final integer with constraints applied:
function GenerateUniqueID() {
return (Math.random() * (78500000 - 78400101) + 78400101)|0;
}
I've programmed this code (javascript countdown) and I have to put 141 of them on page. Doese anybody know if there is some way(program, script etc) that will do the following:
Change from function cdtd1 to function cdtd2 and var sad1 = new Date(); to var sad2 = new Date(); etc.
var d = new Date();
var n = d.getDay();
if(n == 1 || n == 2 || n == 3 || n == 4 || n == 5){
var timer1;
function cdtd1() {
var sad1 = new Date();
var dolazak1 = new Date(sad1.getFullYear(),sad1.getMonth(),sad1.getDate(),23,00,00);
var timeDiff1 = dolazak1.getTime() - sad1.getTime();
if (timeDiff1 <= 0) {
clearInterval(timer1);
$('#dani1Box').remove();
$('#sati1Box').remove();
$('#minute1Box').remove();
$('#sekunde1Box').remove();
}
var sekunde1 = Math.floor(timeDiff1 / 1000);
var minute1 = Math.floor(sekunde1 / 60);
var sati1 = Math.floor(minute1 / 60);
var dani1 = Math.floor(sati1 / 24);
sati1 %= 24;
minute1 %= 60;
sekunde1 %= 60;
$("#dani1Box").html(dani1);
$("#sati1Box").html('7-Dubrava ' + sati1 + ':');
$("#minute1Box").html(minute1 + ':');
$("#sekunde1Box").html(sekunde1);
timer1 = setTimeout(cdtd1, 1000);
}
$(document).ready(function() {
cdtd1();
});
}
I believe what you are looking for is a javscript looping operation.
for(var i = 1; i <= 141; i++) {
console.log(i);
// put code in here that has to run 141 times modifying the html target elements by using string concatenation
$('#target' + i); // This would be come #target1, #target2, #target3 etc up to 141
}
You ask specifically about the variable renaming which would not be necessary in this case since you are reusing the variable in each pass through the loop.
Since you are working with time information you may want to check out this Javascript library: http://momentjs.com/ and work through some of the information on this specific section: http://momentjs.com/docs/#/durations/
JetBrains Webstorm has great javascript refactoring features, including renaming of objects.