Find Server Seed from Hash Verification Script - javascript

View this jsfiddle: https://jsfiddle.net/1L1uqcgv/6/
function refreshTable(){
var hash = document.getElementById("gameHash").value;
var lastHash = "";
var amount = document.getElementById("gameAmount").value;
var tableBody = document.getElementById("tbody");
tableBody.innerHTML = "";
for(var i=0; i<amount; i++){
var gameHash = (lastHash!=""?genGameHash(lastHash):hash);
var gameCrash = crashPointFromHash((lastHash!=""?genGameHash(lastHash):hash));
var clr = gameCrash > 1.97 ? 'green': (gameCrash < 1.97 ? 'red' : 'blue');
tableBody.innerHTML += "<tr><td>"+gameHash+"</td><td style='background:" + clr + "'>"+gameCrash+"</td></tr>";
lastHash = gameHash;
}
}
function divisible(hash, mod) {
// 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 genGameHash(serverSeed) {
return CryptoJS.SHA256(serverSeed).toString()
};
function hmac(key, v) {
var hmacHasher = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, key);
return hmacHasher.finalize(v).toString();
}
function crashPointFromHash(serverSeed) {
// see: provably fair seeding event
var hash = hmac(serverSeed, '000000000000000007a9a31ff7f07463d91af6b5454241d5faf282e5e0fe1b3a');
// In 1 of 101 games the game crashes instantly.
if (divisible(hash, 101))
return 0;
// Use the most significant 52-bit from the hash to calculate the crash point
var h = parseInt(hash.slice(0,52/4),16);
var e = Math.pow(2,52);
return (Math.floor((100 * e - h) / (e - h))/100).toFixed(2);
};
Is it possible to find the "serverSeed" as shown in lines 31 and 41?
To find previous games, a hash is entered into the box, showing all previous games. Would serverSeed be used to find these hashes, or does the single hash create all previous hashes?

The ServerSeed is the hash you input in order to see the crash point and to view the previous games.

Related

Browse is slowing down for a JavaScript code

I'm working on a JavaScript library for me. This library will help you to convert a binary number into a decimal number. The default JavaScript function can only convert before point (integers). But this code will convert floating numbers also (hopefully).
In order to test this when I ran this code on Firefox returned,
This page is slowed down your browser.
& Google Chrome returned nothing but just loading.
So I want to know what is the problem??
Here is my code
var x = "101.11";
var r = 0;
var ra = 0;
for (var i = 0; i < x.length; ++i) {
if (x.charAt(i) == ".") {
var Ap = i;
}
}
for (var j = 0; j < (x.length - Ap); ++j) {
var a = x.charAt(j);
r = r + a * Math.pow(2, ((x.length - Ap - 1) - j));
}
for (var k = Ap + 1;
(x.length - Ap) < k; ++k) {
var b = x.charAt(k);
ra = ra + b * Math.pow(2, (Ap - k));
}
document.write(r);
if (ra <= 0) {
document.write("." + ra);
}
Here is the solution. I'm very much thankful to you people.
/*
It's a dumb library for converting binary fraction number into a desimal number.
Created by : Adib Rahman
Last update : 12:36 30/01/2020
*/
function print(something)
{
document.write(something);
}
var x = "010101010.111";
var r = 0;
var ra = 0;
for(var i=0;i<x.length;++i)
{
if(x.charAt (i)== ".")
{
var Ap=i;
}
}
var beforePoint = (x.length-(x.length-Ap-1))-1;
var afterPoint = x.length-Ap-1;
for(var j=0;j<beforePoint;++j)
{
r = r+(x.charAt(j))*Math.pow(2,(beforePoint-j-1));
}
for(var k=Ap+1;k<x.length;++k)
{
var ra = ra+(x.charAt(k))*Math.pow(2,(Ap-k));
}
//Final Result
if(ra >=0)
{
print(r+ra);
}
else
{
print(r);
}

Coding a hidden message in image

I want to code a hidden message in image using js, but don`t know how this works. I have been searching for some algorithm but dont found one. Can some one explain how to encode message in image using js?
Use this library https://www.peter-eigenschink.at/projects/steganographyjs/
It will allow you to hide text into images
EDIT - Adding the encoding code from the link
Cover.prototype.encode = function(message, image, options) {
// Handle image url
if(image.length) {
image = util.loadImg(image);
} else if(image.src) {
image = util.loadImg(image.src);
} else if(!(image instanceof HTMLImageElement)) {
throw new Error('IllegalInput: The input image is neither an URL string nor an image.');
}
options = options || {};
var config = this.config;
var t = options.t || config.t,
threshold = options.threshold || config.threshold,
codeUnitSize = options.codeUnitSize || config.codeUnitSize,
prime = util.findNextPrime(Math.pow(2,t)),
args = options.args || config.args,
messageDelimiter = options.messageDelimiter || config.messageDelimiter;
if(!t || t < 1 || t > 7) throw new Error('IllegalOptions: Parameter t = " + t + " is not valid: 0 < t < 8');
var shadowCanvas = document.createElement('canvas'),
shadowCtx = shadowCanvas.getContext('2d');
shadowCanvas.style.display = 'none';
shadowCanvas.width = options.width || image.width;
shadowCanvas.height = options.height || image.height;
if(options.height && options.width) {
shadowCtx.drawImage(image, 0, 0, options.width, options.height );
} else {
shadowCtx.drawImage(image, 0, 0);
}
var imageData = shadowCtx.getImageData(0, 0, shadowCanvas.width, shadowCanvas.height),
data = imageData.data;
// bundlesPerChar ... Count of full t-bit-sized bundles per Character
// overlapping ... Count of bits of the currently handled character which are not handled during each run
// dec ... UTF-16 Unicode of the i-th character of the message
// curOverlapping ... The count of the bits of the previous character not handled in the previous run
// mask ... The raw initial bitmask, will be changed every run and if bits are overlapping
var bundlesPerChar = codeUnitSize/t >> 0,
overlapping = codeUnitSize%t,
modMessage = [],
decM, oldDec, oldMask, left, right,
dec, curOverlapping, mask;
var i, j;
for(i=0; i<=message.length; i+=1) {
dec = message.charCodeAt(i) || 0;
curOverlapping = (overlapping*i)%t;
if(curOverlapping > 0 && oldDec) {
// Mask for the new character, shifted with the count of overlapping bits
mask = Math.pow(2,t-curOverlapping) - 1;
// Mask for the old character, i.e. the t-curOverlapping bits on the right
// of that character
oldMask = Math.pow(2, codeUnitSize) * (1 - Math.pow(2, -curOverlapping));
left = (dec & mask) << curOverlapping;
right = (oldDec & oldMask) >> (codeUnitSize - curOverlapping);
modMessage.push(left+right);
if(i<message.length) {
mask = Math.pow(2,2*t-curOverlapping) * (1 - Math.pow(2, -t));
for(j=1; j<bundlesPerChar; j+=1) {
decM = dec & mask;
modMessage.push(decM >> (((j-1)*t)+(t-curOverlapping)));
mask <<= t;
}
if((overlapping*(i+1))%t === 0) {
mask = Math.pow(2, codeUnitSize) * (1 - Math.pow(2,-t));
decM = dec & mask;
modMessage.push(decM >> (codeUnitSize-t));
}
else if(((((overlapping*(i+1))%t) + (t-curOverlapping)) <= t)) {
decM = dec & mask;
modMessage.push(decM >> (((bundlesPerChar-1)*t)+(t-curOverlapping)));
}
}
}
else if(i<message.length) {
mask = Math.pow(2,t) - 1;
for(j=0; j<bundlesPerChar; j+=1) {
decM = dec & mask;
modMessage.push(decM >> (j*t));
mask <<= t;
}
}
oldDec = dec;
}
// Write Data
var offset, index, subOffset, delimiter = messageDelimiter(modMessage,threshold),
q, qS;
for(offset = 0; (offset+threshold)*4 <= data.length && (offset+threshold) <= modMessage.length; offset += threshold) {
qS=[];
for(i=0; i<threshold && i+offset < modMessage.length; i+=1) {
q = 0;
for(j=offset; j<threshold+offset && j<modMessage.length; j+=1)
q+=modMessage[j]*Math.pow(args(i),j-offset);
qS[i] = (255-prime+1)+(q%prime);
}
for(i=offset*4; i<(offset+qS.length)*4 && i<data.length; i+=4)
data[i+3] = qS[(i/4)%threshold];
subOffset = qS.length;
}
// Write message-delimiter
for(index = (offset+subOffset); index-(offset+subOffset)<delimiter.length && (offset+delimiter.length)*4<data.length; index+=1)
data[(index*4)+3]=delimiter[index-(offset+subOffset)];
// Clear remaining data
for(i=((index+1)*4)+3; i<data.length; i+=4) data[i] = 255;
imageData.data = data;
shadowCtx.putImageData(imageData, 0, 0);
return shadowCanvas.toDataURL();
};

How to display output of one function after another

I have written a Javascript file of two algorithms. As shown in the code below, I am using a for loop to generate random values which are used by both algorithms as input.
At present, I am displaying output of the binarySearch and SearchSorted alternatively.
The problem I am facing is I have to pass the same array values generated by randomlyGenerateArray in the main program to both the algorithms for a meaningful comparison. But I don't know how to change the output format.
I have thought of adding them in different loops, but as I have explained above i need to use the same randomArray values for both the algorithms.
i.e., The below code produces output as shown below -
Binary Search Successful 1
Search Sorted Successful 5
Binary Search Successful 3
Search Sorted Successful 10
How do I display the output of Binary Search First and then display output of Search Sorted? it's something like this. Any help will be greatly appreciated.
Binary Search Successful 1
Binary Search Successful 3
Search Sorted Successful 5
Search Sorted Successful 10
// Binary Search Algorithm
function binarySearch(A,K)
{
var l = 0; // min
var r = A.length - 1; //max
var n = A.length;
var operations = 0;
while(l <= r)
{
var m = Math.floor((l + r)/2);
operations++;
if(K == A[m])
{
console.log('Binary Search Successful %d',operations);
return m;
}
else if(K < A[m])
{
r = m - 1;
}
else
{
l = m + 1;
}
}
operations++;
console.log('Binary Search Unsuccessful %d',operations);
return -1;
}
// Search Sorted Algorithm
function searchSorted(A, K)
{
var n = A.length;
var i = 0;
var operations = 0;
while (i < n)
{
operations++;
if (K < A[i])
{
return -1;
}
else if (K == A[i])
{
console.log('Search Sorted Successful %d', operations);
return i;
}
else
{
i = i + 1;
}
}
operations++;
console.log('Search Sorted Unsuccessful %d', operations);
return -1;
}
// Random Array generator
var randomlyGenerateArray = function(size)
{
var array = [];
for (var i = 0; i < size; i++)
{
var temp = Math.floor(Math.random() * maxArrayValue);
var final = array.splice(5, 0, 30);
array.push(final);
}
return array;
}
//Sort the Array
var sortNumber = function(a, b)
{
return a - b;
}
// Main Program
var program = function()
{
var incrementSize = largestArray / numberOfArrays;
for (var i = smallestArray; i <= largestArray; i += incrementSize)
{
var randomArray = randomlyGenerateArray(i);
var sort = randomArray.sort(sortNumber);
var randomKey = 30;
binarySearch(sort, randomKey);
searchSorted(sort, randomKey);
}
}
var smallestArray = 10;
var largestArray = 10000;
var numberOfArrays = 1000;
var minArrayValue = 1;
var maxArrayValue = 1000;
program();
You could store the sorted randomArrays in an array (which I've called sortedRandomArrays), then run a for loop for each search.
The Main Program would then look like:
// Main Program
var program = function()
{
var incrementSize = largestArray / numberOfArrays;
var sortedRandomArrays = [];
for (var i = smallestArray; i <= largestArray; i += incrementSize)
{
var randomArray = randomlyGenerateArray(i));
var sort = randomArray.sort(sortNumber);
sortedRandomArrays.push(sort);
var randomKey = 30;
}
for (var i = 0; i < sortedRandomArrays.length; i++)
{
binarySearch(sortedRandomArrays[i], randomKey);
}
for (var i = 0; i < sortedRandomArrays.length; i++)
{
searchSorted(sortedRandomArrays[i], randomKey);
}
}
Solution is simple: store the results and print with 2 separate loops (take out the printing from within the functions).
var program = function()
{
var binarySearchResults = [];
var sortedSearchResults = [];
var incrementSize = largestArray / numberOfArrays;
for (var i = smallestArray; i <= largestArray; i += incrementSize)
{
var randomArray = randomlyGenerateArray(i);
var sort = randomArray.sort(sortNumber);
var randomKey = 30;
binarySearchResults[i] = binarySearch(sort, randomKey);
sortedSearchResults[i] = searchSorted(sort, randomKey);
}
for (var i = smallestArray; i <= largestArray; i += incrementSize)
{
//print binary results
}
for (var i = smallestArray; i <= largestArray; i += incrementSize)
{
//print sorted results
}
}

Execution time for successful search Binary Search algorithm

I have implemented Binary Search Algorithm using Node.js. I am recording the time taken by the algorithm to search for a number in a random generated array. I am able to output the time taken by the algorithm for an unsuccessful search.
But I am not able to figure out how to measure the time taken by the algorithm to successfully search a number in an array.
Here is my code -
function binarySearch(A,K)
{
var l = 0; // min
var r = A.length - 1; //max
var n = A.length;
var time = process.hrtime();
while(l <= r)
{
var m = Math.floor((l + r)/2);
if(K == A[m])
{
return m;
}
else if(K < A[m])
{
r = m - 1;
}
else
{
l = m + 1;
}
}
time = process.hrtime(time);
console.log('%d',time[1]/1000000);
return -1;
}
var randomlyGenerateArray = function(size)
{
var array = [];
for (var i = 0; i < size; i++)
{
var temp = Math.floor(Math.random() * maxArrayValue);
array.push(temp);
}
return array;
}
var sortNumber = function(a, b)
{
return a - b;
}
var program = function()
{
for (var i = 0; i <= 10000; i += 10)
{
var randomArray = randomlyGenerateArray(i);
var sort = randomArray.sort(sortNumber);
var randomKey = 100;
var result = binarySearch(sort, randomKey);
if(result < 0)
{
console.log("Element not found");
}
else
{
console.log('Element found in position ',result);
}
}
}
var maxArrayValue = 1000;
program();
I am using var time = process.hrtime(); to start the timer at the beginning of the algorithm and using time = process.hrtime(time); to end the timer and output it in the console.
How can I measure the time taken by the algorithm to successfully search a number in an array.
Any help would be greatly appreciated.
Start your timer before calling the binary search function and end it after the call .. regardless of whether the search is successful or not, you will get the time ..
var time = process.hrtime();
var result = binarySearch(sort, randomKey);
time = process.hrtime(time);
......

Why does the following Short ID generator always pause after a specific number of iterations

I am writing my own short Id generator. Running into some trouble testing the anticipated number of clashes, the script always seems to pause after a certain number of iterations. It prints 3.6999999999999997% clashes = 0 (that is after 37,000,000 iterations) and then pauses for a long time before printing 3.8% clashes = 0.
I have no real idea why this is?
var alreadyExistsCounter = 0;
var oneMillion = 1000000;
var iterations = oneMillion * 100;
var ALPHABET = '23456789abdegjkmnpqrvwxyz';
var hash = {};
var ID_LENGTH = 10;
var generate = function() {
var rtn = '';
for (var i = 0; i < ID_LENGTH; i++) {
rtn += ALPHABET.charAt(Math.floor(Math.random() * ALPHABET.length));
}
return rtn;
}
for (var i = 0; i < iterations;i++){
var sh = generate();
if (hash[sh]){
alreadyExistsCounter = alreadyExistsCounter +1;
}
hash[sh] = true;
if (i % 100000 === 0){
var pct = i / iterations * 100;
console.log( pct + '% clashes = ' + alreadyExistsCounter );
}
}

Categories