简体   繁体   English

如何使用Javascript或Flash获取用户文件的哈希值?

[英]How would I get a Hash value of a users file with Javascript or Flash?

What I need to do is provide a file browser box on a web page where a user selects a file from their computer and, through javascript (or flash if necessary), some sort of Hash is returned such as CRC, MD5 or SHA1. 我需要做的是在网页上提供一个文件浏览器框,用户从他们的计算机中选择一个文件,并通过javascript(或必要时闪存),返回某种类型的哈希,如CRC,MD5或SHA1。 I would prefer not to have to upload the entire file to the web server, as I expect some rather large files to be used in this. 我不希望不必将整个文件上传到Web服务器,因为我希望在此使用一些相当大的文件。

Basically I am making a script that associates these values with META data of sorts, allowing the files to be identified without having to be uploaded completely. 基本上我正在编写一个脚本,将这些值与各种META数据相关联,从而可以在不必完全上传的情况下识别文件。

Any idea how I would go about doing this? 知道怎么做这个吗? It'd be easy for me to do it on the server side, but, as I said, there will be some rather large files checked and I don't want to eat up too much of the server's bandwidth. 我可以很容易地在服务器端执行此操作,但是,正如我所说,将会检查一些相当大的文件,我不想吃太多的服务器带宽。

You can do it with Flash, provided that the user has Flash Player 10. 只要用户具有Flash Player 10,您就可以使用Flash。

Here is a tutorial 这是一个教程

Also: Reading and writing local files in FP10 另外: 在FP10中读写本地文件

This is traditionally not possible with JavaScript, but it may be if the W3 File Upload spec ever catches on. 传统上这对JavaScript来说是不可能的,但可能是因为W3文件上传规范已经出现。

A variant is available in Firefox 3: Firefox 3中提供了一种变体:

var content= input.files[0].getAsBinary();

For other browsers you would have to fall back to Flash and/or server-side hashing. 对于其他浏览器,您必须回退到Flash和/或服务器端散列。

Here's a bonus JS implementation of SHA-1 for you: 这里有一个针对SHA的奖励JS实现:

function sha_hexdigest(bytes) {
  var digest= sha_bytes(sha_calculate(sha_ints(bytes), bytes.length*8));
  var digits= '0123456789abcdef';
  var hex= '';
  for (var i= 0; i<digest.length; i++) {
    var c= digest.charCodeAt(i);
    hex+= digits.charAt((c>>4)&0xF) + digits.charAt(c&0xF);
  }
  return hex;
}

function sha_ints(bytes) {
  while (bytes.length%4!=0)
    bytes+= '\x00';
  var ints= new Array();
  for (var i= 0; i<bytes.length; i+= 4) {
    ints[ints.length]= (
      (bytes.charCodeAt(i)&0xFF)<<24 | (bytes.charCodeAt(i+1)&0xFF)<<16 |
      (bytes.charCodeAt(i+2)&0xFF)<<8 | (bytes.charCodeAt(i+3)&0xFF)
    ); }
  return ints;
}

function sha_bytes(ints) {
  var bytes= '';
  for (var i= 0; i<ints.length; i++)
    bytes+= String.fromCharCode((ints[i]>>24)&0xFF, (ints[i]>>16)&0xFF, (ints[i]>>8)&0xFF, ints[i]&0xFF)
  return bytes;
}

function sha_calculate(ints, bitn) {
  while (ints.length*32<=bitn) ints[ints.length]= 0;
  ints[ints.length-1]|= 1<<(31-bitn%32)
  while (ints.length%16!=14) ints[ints.length]= 0;
  ints[ints.length]= Math.floor(bitn/0x100000000);
  ints[ints.length]= bitn&0xFFFFFFFF;

  var h0= 1732584193, h1= -271733879, h2= -1732584194, h3= 271733878, h4= -1009589776;
  var a, b, c, d, e, f, k, temp, w= new Array(80);
  for(var inti= 0; inti<ints.length; inti+= 16) {
    a= h0; b= h1; c= h2; d= h3; e= h4;
    for (var i= 0; i<16; i++) w[i]= ints[inti+i];
    for (; i<80; i++) w[i]= sha_rol(w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16], 1);

    for(var i= 0; i<80; i++) {
      switch (Math.floor(i/20)) {
        case 0: f= sha_add(1518500249, (b & c) | ((~b) & d)); break;
        case 1: f= sha_add(1859775393, b ^ c ^ d); break;
        case 2: f= sha_add(-1894007588, (b & c) | (b & d) | (c & d)); break;
        case 3: f= sha_add(-899497514, b ^ c ^ d); break;
      }
      temp= sha_add( sha_add(sha_rol(a, 5), w[i]), sha_add(e, f) );
      e= d; d= c; c= b; b= a; a= temp;
      c= sha_rol(c, 30);
    }

    h0= sha_add(h0, a); h1= sha_add(h1, b); h2= sha_add(h2, c); h3= sha_add(h3, d); h4= sha_add(h4, e);
  }
  return new Array(h0, h1, h2, h3, h4);
}

function sha_add(a, b) {
  var lsw= (a&0xFFFF) + (b&0xFFFF);
  var msw= (a>>16) + (b>>16) + (lsw>>16);
  return (msw<<16) | (lsw&0xFFFF);
}

function sha_rol(n, bits) {
  return (n<<bits) | (n>>>(32-bits));
}

使用HTML5 File API,您可以在本地计算机中读取文件,擅长我的解决方案: 在Javascript中使用HTML5文件API生成SHA1文件校验和

This is not possible with Javascript because of there is no access to the file system. 使用Javascript无法实现这一点,因为无法访问文件系统。 I know this can be done with Silverlight. 我知道这可以用Silverlight完成。 Perhaps someone with flash experience can answer. 也许拥有闪存经验的人可以回答。

Browsers (ie JavaScript) cannot access the local file system, period. 浏览器(即JavaScript)无法访问本地文件系统,期间。 If they could, it would be a security nightmare. 如果他们可以,那将是一场安全噩梦。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建将根据用户Flash Player版本提供不同SWF的Javascript - how to create such Javascript that would provide different swfs depending on users flash player version 如何使用此代码在 JavaScript 的屏幕上为 flash 2 个不同的 colors 设置精灵的动画? 或者更确切地说,我将如何解决它? - How would I animate a sprite to flash 2 different colors on screen in JavaScript, with this code? Or rather how would I fix it? Javascript-从Flash获取价值 - Javascript - get value from Flash 我如何引用刚刚在Javascript中创建并获得其值的特定html元素? - How would I refer to a specific html element that was just made in Javascript and get its value? 如何从 javascript 中的 map 中的异步 function 中获取产品价值 - How would I get the product value out of a async function inside a map in javascript 我将如何使用 javascript 或 dojo 从链接或按钮实现“将 url 复制到剪贴板”而不使用 Flash - How would I implement 'copy url to clipboard' from a link or button using javascript or dojo without flash 如何获取使用Javascript在多文件输入中选择的文件数? - How would I get the number of files selected in a multi file input with Javascript? 如何在 js 模块代码中获取 webpack output 文件名和 hash 值? - How do I get the webpack output file name with hash value in the js module code? 我如何获得Flash的真实网址 <object> 通过JavaScript? - How can i get the Flash real url in <object> by JavaScript? Javascript闪存卡脚本如何获得切换? - Javascript flash card script how can I get it to toggle?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM