Gyh's blog

vuePress-theme-reco gyh    2022
Gyh's blog Gyh's blog

Choose mode

  • dark
  • auto
  • light
Home
Category
  • Algorithm
  • CSS
  • JavaScript
  • Others
  • Server
  • Utils
  • Article
  • Note
  • Git
  • Npm
  • Standard
  • Summary
Tag
Timeline
About
GitHub
author-avatar

gyh

91

Article

11

Tag

Home
Category
  • Algorithm
  • CSS
  • JavaScript
  • Others
  • Server
  • Utils
  • Article
  • Note
  • Git
  • Npm
  • Standard
  • Summary
Tag
Timeline
About
GitHub

使用 spark-md5 对文件进行加密

vuePress-theme-reco gyh    2022

使用 spark-md5 对文件进行加密

gyh 2018-09-09

工具:spark-md5

md5 加密字符串

var hexHash = SparkMD5.hash("mypridelife")
1

md5 加密文件代码

    getMd5(file, callBack) {
      var tmp_md5
      var fileReader = new FileReader()
      var blobSlice =
        File.prototype.mozSlice ||
        File.prototype.webkitSlice ||
        File.prototype.slice
      var chunkSize = 2097152
      // read in chunks of 2MB
      var chunks = Math.ceil(file.size / chunkSize)
      var currentChunk = 0
      var spark = new SparkMD5()

      fileReader.onload = function(e) {
        spark.appendBinary(e.target.result) // append binary string
        currentChunk++

        if (currentChunk < chunks) {
          loadNext()
        } else {
          tmp_md5 = spark.end()
          callBack(tmp_md5)
        }
      }

      function loadNext() {
        var start = currentChunk * chunkSize
        var end = start + chunkSize >= file.size ? file.size : start + chunkSize

        fileReader.readAsBinaryString(blobSlice.call(file, start, end))
      }

      loadNext()
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Edit on GitHub~
LastUpdated: 5/17/2022, 8:59:22 AM