Giter Site home page Giter Site logo

download's Introduction

rndme

unpredictable number generation

npm install rndme - or - bower install rndme

Check out a live demo to see what the fuss is about.

Also drives my bulk-random file generator

Purpose

Easily gather numbers or text from sound, movement, video, and more.

Cryptography needs un-predictable keys to ensure security. Recent revelations have cast doubt about window.crypto, and everyone knows Math.random() is useless. Existing methods of "entropy gathering" like mouse jiggling or typing are akward for users and hampered by mobile devices. With uniform access to a variety of sources, rndme offers you simple and felxible creation of un-predictable and uniformly-distributed data, if you're into that sort of thing.

Input Sources

Sound

Uses getUserMedia() to capture numbers from a microphone. This is the fastest physical source of numbers at over 900 chars per ms sustained. The raw samples are not only shuffled, they are cropped of the most and least signifigant digits, so there's no telling what was briefly "recorded". Use medium volume music or static for a more even distribution of output.

Video

Uses getUserMedia() to capture numbers from a camera. Thousands of pixels are rolled into each character of output. Move the camera around and have some color contrast in the video for maximum entropy. Video is the 2nd fastest source of numbers after sound at around 3 - 80 chars per ms, depending on brightness and movement. Even a dark camera will work, but one shooting the floor or horizon will produce data much faster.

Motion

Motion uses the device Motion API to turn physical movements into numbers. It shuffles the samples and chops off bits to ensure privacy in the face of low-resolution sensors and motionless capture. Best entropy is obtained with lots of movement starting and stopping, like gently shaking while tilting. Motion's performance varies per device but ranges from about 2.5 - 5 chars per ms.

Time

Time uses a high-resolution clock and a random workload to gather numbers. Since it uses a clock and rolls many workload timings into each output sample, it's rather slow compared to the others at around 0.60 chars per ms. Time has one main advantage: it works on desktop and mobile alike and doesn't need user permissions. It's great for shorter chunks of data like salts, symmetric encryption keys, and entropy for other number generators.

Crypto

Crypto uses crypto.getRandomValues and a high-resolution clock derivative to gather numbers. This method is sync under-the-hood and is directly callable. OS-provided numbers are muliplied by a number derived from the date and a high-resolution performance timing API, then cropped in the middle to deliver un-compromised randomness. The crypto source's exact performance rate depends on CPU speed and OS-provided entropy, but expect 100 - 500 chars per ms.

Combo

Combo mixes output from one or more sources, like sound and motion, to ensure randomness in the face of broken sensors, or just for plain good measure. It's an easy way to address concerns about one source or the other: use both! All data is gathered at the same time, so you only have to wait the time of the slowest source used. rndme.combo takes a special first argument, a list of source names, but the other parameters are the same except progress, which is not available on combo.

Output Formats

Format Seperator Range Description
hex "" 0-F hex-encoded byte values
bytes "," 0-255 integers that fit into 1 byte
bin "" \u0-\u255 binary string of bytes
int "" 0-9 continuous digits
base64 "" \--z URL-safe chars
base92 "" \t-~ JSON-safe chars
float "," 0-1 16 digit floats
raw "," 0-1 same as int, but no limit

Usage

Static methods on the rndme variable can be used async with callbacks or promises.

//   source ( format  limit  callback  progess ) -or- .then(callback)
rndme.sound("bytes", 12345, function(s){alert(s)});
rndme.motion("hex", 1024,function(s){alert(s)}, console.info.bind(console));
rndme.time("float", 256,function(s){alert(s)}, console.info.bind(console));
rndme.video("base92", 1024).then(alert).catch(confirm);
rndme.combo(["sound", "video", "motion"], "hex", 2000).then(alert); // combine 3 sources
rndme.combo("video", "hex", 2000, alert); // combine one source (alt syntax + cb)

Static Utilities

stamp

._stamp() - returns 10 random digits, sync, based on the current time. Using Date()s and high-resolution timingings with a chunk of slower code internally, this method should produce different output each time it's called:

[1,2,3,4,5].map(rndme._stamp);
// == ["1621049878", "7138172444", "5275617627", "1540339147", "2792212006"]

spin

._spin(arrToShuffle, optNumSwaps) - re-arranges the elements in an array into an unpredictable order bu swapping values. The default is as many swaps as elements, but a 2nd argument can specify a custom number of swaps if desired. Uses Math.random(), so it should not be a primary source of sectrets, but it can help re-arrange secrets to provide a better distribution.

rndme._spin([1,2,3,4,5,6,7,8,9]);
// ~==[5, 2, 8, 9, 6, 3, 1, 7, 4] // note complete re-distribution of element order

munge

._munge - An [].sort() callback that contrary to popular belief, does NOT randomize an array. It does slightly re-arrange the elements, and it does so quickly. It's used by rndme to prevent repeated output in very low-entroy situations like a user having a broken camera and using the video input by altering the look up table of some output builders prior to each execution.

[1,2,3,4,5,6,7,8,9].sort(rndme._munge);
// ~== [2, 1, 8, 3, 7, 4, 5, 6, 9]; // note different order, but not complete re-distribution

combine

._combine(inp1, inp2) - Given two strings or arrays of digits, add them together as numbers and keep the least significant digits. This is used internaly by the combo source, but is exposed for mixing other data/entropy as needed.

rndme._combine("123","789");
// == "802"  (1+7=8, 2+8=0, 3+9=2)

test results on 50,000 bin samples

tested using http://fourmilab.ch/random/ 's ent.exe program on windows

audio

Entropy = 7.986279 bits per byte.
Optimum compression would reduce the size of this 50000 byte file by 0 percent.

Chi square distribution for 50000 samples is 903.65, and randomly would exceed this value less than 0.01 percent of the times.

Arithmetic mean value of data bytes is 127.3347 (127.5 = random).
Monte Carlo value for Pi is 3.139325573 (error 0.07 percent).
Serial correlation coefficient is -0.004064 (totally uncorrelated = 0.0).

video (w/camera obscured, no motion)

Entropy = 7.990584 bits per byte.
Optimum compression would reduce the size of this 50000 byte file by 0 percent.

Chi square distribution for 50000 samples is 589.68, and randomly would exceed this value less than 0.01 percent of the times.

Arithmetic mean value of data bytes is 127.6303 (127.5 = random).
Monte Carlo value for Pi is 3.142205688 (error 0.02 percent).
Serial correlation coefficient is -0.000538 (totally uncorrelated = 0.0).

crypto

Entropy = 7.980536 bits per byte.
Optimum compression would reduce the size of this 49895 byte file by 0 percent.

Chi square distribution for 49895 samples is 1118.04, and randomly would exceed this value less than 0.01 percent of the times.

Arithmetic mean value of data bytes is 127.7410 (127.5 = random).
Monte Carlo value for Pi is 3.151413109 (error 0.31 percent).
Serial correlation coefficient is -0.012716 (totally uncorrelated = 0.0).

download's People

Contributors

ciaranj avatar extend1994 avatar glittle avatar jantimon avatar jpstevens avatar msokk avatar rndme avatar sliekens avatar x-raym avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

download's Issues

Safari Issue

I saw that for safari the image is opened in the tab and the user should save it manually which is acceptable. My problem is that when i have multiple images and i call download function many times one after the other , the confirm popup appears as many times as images i have and i have to press ok for every each of them, but the main problem is that I can see only the last image, all the others are lost.

An error in IE10-11: InvalidStateError

Here is a fix, taken from bpampuch/pdfmake#294:

line 82:
try {
blob = payload instanceof myBlob ?
payload :
new myBlob([payload], {type: mimeType}) ;
}
catch (e) {
// Old browser, need to use blob builder
window.BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder;
if (window.BlobBuilder) {
var bb = new BlobBuilder();
bb.append(payload);
blob = bb.getBlob(mimeType);
}
}

Download image/jpeg image from a Buffer object

I'm trying to download a image file from a JSON object:

{"type":"Buffer","data":[255,216,255,224,0,16,74,70,7...]}

download(new Blob(buffer.data, {type: "image/jpeg"}), "image.jpeg", "image/jpeg");

but the downloaded file is not valid image. Do i have to perform previous convertion with the Buffer object? any help?

EDIT: my bad.
The `"data":[255,216,255,224,0,16,74,70,7...]}`` from the JSON object is not a actually Buffer, just a String representation of it. Changed my API to return a Base64 String of the file.

Bower

Hello,
Your lib is very cool, you should publish it into the bower repository, so we will include easily in our project.

good job ๐Ÿบ

Set Folder to download

How can I set a specific folder to download the file, instead of the default folder "Downloads"?

Silent Mode

Is it possible to hide the message of download (silent mode)!

Issue with mp4 download and filename parameter

When I download an mp4 with only a single parameter all is good:
download(highestQuality);
If I add a filename parameter it downloads a text file with the URL as the contents:

downloadName = videoName + '.mp4';
download(highestQuality,downloadName);

I tried using a static string for the name and added the mimetype, but neither helped.

Am I misunderstanding something, or is this a limitation?
Thanks-
Matt

Cannot open PDF file after download

Please help to check, this is my code:

  var fileUrl = form.vals().download;
  var fileName = fileUrl ? fileUrl.replace(/^.*[\\\/]/, '') : '';
  download(fileUrl, fileName, "application/pdf");

After download, that file can not open:
image

Not all content types described by RFC 2045 are supported when using data urls

As described by RFC 2045, all US-ASCII characters except SPACE, CTLs or tspecials are allowed in the content type.

The regular expression detecting data url usage only checks for words (\w) and dashes (\-).

How to reproduce:
Use a data url with a content type containing a dot (.) character.

For example:
data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,<BASE64_ENCODED_DATA>

error: download is not a function

I don't know how to describe it properly, and neither do I have a MRE.
But it seems that if the main function is called "download", that creates an error as described in the title.

I have renamed your function to something else (e.g. "downloadit") and then it worked.

No idea what might be triggering this error (I am using OS X version 10.9.5 and Chrome Version 64.0.3282.167), but I hope this might be of help somehow.

NPM Package

Would love an NPM package for those of us that use it!

Chrome breaks if base64 url is bigger than 2MB

This is quite an old bug but it looks like it has not been resolved yet. Basically when I try to use your plugin to download a base64 that is bigger than several MB (from a canvas), I get "network error" instead of the proper file.
Probably you should switch to Blob if the base64 string is too long.

Cross-Origin Issue

For all other request cross origin issue doesn't come as cors is already implemented in node side but when i use download(url). It throws cors error?
fas

Safari 7.1 download

I've tried setting up a simple string download which works fine in Chrome and Firefox but not in Safari 7.1. After triggering the download I can see the data being inserted into the current page real quick and then it disappears.

download('This is some string', filename, 'text/plain');

Support download of MS Office docx and xlsx files

Thank you for this great resource. I'm using it to download files returned from a request that uses a proxy and the link to the documents cannot simply be placed in an <a href>

I can successfully download all file types with the exception of the microsoft office docx and xlsx types. When attempting to open the downloaded files with MS Office I get an error message stating that they are corrupt and cannot be repaired.

I have tried including the correct mime type
application/vnd.openxmlformats-officedocument.wordprocessingml.document

AND

leaving the mime type blank (and defaulting to application/octet-stream)

Both result in corrupted file.

Any ideas how to support the download of those file types correctly?

my code:

var x = new XMLHttpRequest();
 x.open("GET", attachUrl, true);
 x.responseType = 'blob';
// with correct mime type
//x.onload = function (e) { download(x.response, name, file.ContentType); }
// without mime type
x.onload = function (e) { download(x.response, name); }
console.log(file.ContentType);
x.send();

and the error message in MS Office
corrupt

How is the message to download file appear

I have been working with this library for last week now. In the beginning the file download message was coming at the bottom of the browser like this
image5

I have seen it change to opening a save as window
image4

and Today it just opens the file in jpeg, doesn't give a window message or show the file download bottom of the file, just simply opens it in photos,

Do we know what drives that behavior?

Thanks

support Chinese link download

change code

if(url && url.length< 2048){ // if no filename and no mime, assume a url was passed as the only argument
			fileName = url.split("/").pop().split("?")[0];
			anchor.href = url; // assign href prop to temp anchor
		  	if(anchor.href.indexOf(url) !== -1){ // if the browser determines that it's a potentially valid url path:
        		var ajax=new XMLHttpRequest();
        		ajax.open( "GET", url, true);
        		ajax.responseType = 'blob';
        		ajax.onload= function(e){ 
				  download(e.target.response, fileName, defaultMime);
				};
        		setTimeout(function(){ ajax.send();}, 0); // allows setting custom ajax headers using the return:
			    return ajax;
			} // end if valid url?
		} // end if url?
fileName = url.split("/").pop().split("?")[0];
url = encodeURI(decodeURI(url));
anchor.href = url

can support Chinese filename

1.4.6+ broke downloading files with non-ASCII charachers

We are using download.js to download CSV/TSV/etc. data from query application, see source code here: https://github.com/wikimedia/wikidata-query-gui/blob/master/wikibase/queryService/ui/App.js#L515

With 1.4.4, it worked just fine. With 1.4.6+, however, the data is downloaded broken - non-ASCII characters are mangled. Looks like some changes in 1.4.6 broke something in non-ASCII download.

The resulting files for comparison:
1.4.4: https://gist.github.com/smalyshev/e4c02c94f1433111f7100836496d7eff
1.4.6+: https://gist.github.com/smalyshev/379b46d1a0b8bec7953f26a7c4261800

I've tested in Chrome, but other browsers are reported by users to produce similar results.

Server problem when loading src within script tag.

Hi there,

Thank you for the nice work, I have been using your download script over 2 years. I recently bumped into an issue in chrome with download.js. When loading from script tag:

<script src="http://danml.com/js/download.js"></script>

chrome seems to wait forever, as a result, page dependent on it will never load unless they are loaded async. Firefox seems to have a cached version in my pc. I cannot seem to connect to your website from chrome either. Are you changing files on the server or is this new behavior intended?

Thank you for your work once again,

download file from URL

Hi,

maybe it's a simple question, but I can't make it myself:

how has the function to look alike if I want to download an existing file?

like the filename is "image.jpg"?

thank you!

Data URI decoder is not selected properly

		function dataUrlToBlob(strUrl) {
			var parts= strUrl.split(/[:;,]/),
			type= parts[1],
			decoder= parts[2] == "base64" ? atob : decodeURIComponent,
			...
		}

https://github.com/rndme/download/blob/master/download.js#L90

This one fails, it is generated with google first hit from text:

data:text/plain;charset=utf-8;base64,w4FydsOtenTFsXLFkXTDvGvDtnJmw7p0w7Nnw6lwLg==

The problem that the charset is given, so the second part is the charset rather than the encoding. You should parse the heading part of the URI instead of making presumptions on its structure.

Great library thanks!

Safari still refuses to download... tries to open up the file in a new tab or window. Any way to workaround this? I need to be able to save a client side code generated file in blob or objectURL format to a user's downloads folder.

Error with Uint8Array input

I got an Exception about saying payload.charCodeAt is not a function at line 78 when I was testing this library with an Uint8Array input.

When downloading from URL, allow custom filename

To download from a URL, currently I have to provide a single argument, which is the URL itself. The filename will be automatically determined from the URL. If I added the second filename argument, downloadjs will treat it like downloading blob url.

I think it should be possible to still treat it like downloading from URL, and use the second argument as filename instead of determining from the URL. Something like:

download('https://example.com/1234-5678-9012.pdf', 'mine.pdf')

Issue in downloading excel sheet.

I am using REST API @ back and it provides the byteStream of the excel file and I am trying to generate file @ client Side.
File is downloaded but the content stores as the same the server responds to the front end.

Expose the limitations programatically

This is a great lib for downloading, event though it is supported in modern browsers some people are in older browser for them i want to show which file is download or not download, to do so expose the limitations via isChromeSupoort,isIESupport like that variables.

Asynchronous?

Hi, thanks for this awesome library. I just have a couple questions.

1.) is the download function asynchronous?
2.) is there a way notify the calling code when it is done?

My motivation for asking is that I want to close the parent window when the download starts or completes.
I know this isn't really an issue with the code, just wondering if I can make it do what I want:)

docx, ods

Hi,
in my app i was using just this piece of js:

var link = document.createElement("a"); link.download = dataItem.Filename; link.href = data; link.click();

where data is the data uri with mime type etc...
this is working perfectly on chrome, but not in ie
so i used your library, and it's perfect for ie
the only issue is that is not working with documents like docx, ods...

Compatibility with IE11?

Arggh, ignore this Issue, I was being a massive tool and had not uploaded the right files to our dev server when testing on IE. Seems to work fine on IE11. I'll delete this Issue if I find out how...

PDF download

Hi,

I have few PDFs available on a URL, and I want to allow user to download them. I tried passing PDF as URL, but it did't work.

In example available on http://pagedemos.com/yvvmxbjrwq7u/2 , I replaced code for image download from:
download( "/diff6.png" );
to
download( "www.example.com/diff6.pdf" );

but it did't work. Do we need to do any changes for PDF to get downloaded successfully?

Thanks.

add download progress

Hi do you think you could add some download progerss in % in console.log as file downloads?

Thanks for Anwsering and Best Regards

Problem with Chrome on iOS

I tried every single demo but neither works on Chrome on iPhone, working on solution for iPhone/iPad download and considering your lib as a potential fix.

Customize behaviour when identifies that the browser is Safari.

Hello everyone!

I want to customize the behaviour when the browser is Safari. Everybody knows that the Safari isn't support the a[download] attribute yet and, i want to handle the behaviour more customizable. For that, I implemented a simple solution for, just passing an option parameter to the download function but i'm in doubt about the flexibility and customizability for that solution.

I'll send a PR but discussion is opened!
Best regards.

Does not seem to work with "audio/mpeg" mime format

Got an audio file coming in through ajax and when ajax receives it the file is the right size with correct headers but once its run through download it almost doubles in size and is not readable by mp3 players.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.