How to generate MD5 in Angular

I’m working on an Angular application and need to generate an MD5 checksum for a file. I found that the SparkMD5 library can be used for this purpose, but I’m not sure how to integrate it into my Angular project.

Could someone provide a step-by-step guide or example on how to achieve this?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>onFileSelected(event: any) {
const file = event.target.files[0];
if (file) {
this.computeMD5(file);
}
}
computeMD5(file: File) {
const fileReader = new FileReader();
fileReader.onload = (e) => {
const arrayBuffer: ArrayBuffer = e.target?.result as ArrayBuffer;
const md5 = SparkMD5.ArrayBuffer.hash(arrayBuffer);
this.fileChecksum = md5;
};
fileReader.readAsArrayBuffer(file);
}
computeChecksumMd5(file: File): Promise<string> {
return new Promise((resolve, reject) => {
const chunkSize = 2097152; // Read in chunks of 2MB
const spark = new SparkMD5.ArrayBuffer();
const fileReader = new FileReader();
let cursor = 0; // current cursor in file
fileReader.onerror = function(): void {
reject('MD5 computation failed - error reading the file');
};
// read chunk starting at `cursor` into memory
function processChunk(chunk_start: number): void {
const chunk_end = Math.min(file.size, chunk_start + chunkSize);
fileReader.readAsArrayBuffer(file.slice(chunk_start, chunk_end));
}
// when it's available in memory, process it
// If using TS >= 3.6, you can use `FileReaderProgressEvent` type instead
// of `any` for `e` variable, otherwise stick with `any`
// See https://github.com/Microsoft/TypeScript/issues/25510
fileReader.onload = function(e: any): void {
spark.append(e.target.result); // Accumulate chunk to md5 computation
cursor += chunkSize; // Move past this chunk
if (cursor < file.size) {
// Enqueue next chunk to be accumulated
processChunk(cursor);
} else {
// Computation ended, last chunk has been processed. Return as Promise value.
// This returns the base64 encoded md5 hash, which is what
// Rails ActiveStorage or cloud services expect
resolve(btoa(spark.end(true)));
// If you prefer the hexdigest form (looking like
// '7cf530335b8547945f1a48880bc421b2'), replace the above line with:
// resolve(spark.end());
}
};
processChunk(0);
});
}
</code>
<code>onFileSelected(event: any) { const file = event.target.files[0]; if (file) { this.computeMD5(file); } } computeMD5(file: File) { const fileReader = new FileReader(); fileReader.onload = (e) => { const arrayBuffer: ArrayBuffer = e.target?.result as ArrayBuffer; const md5 = SparkMD5.ArrayBuffer.hash(arrayBuffer); this.fileChecksum = md5; }; fileReader.readAsArrayBuffer(file); } computeChecksumMd5(file: File): Promise<string> { return new Promise((resolve, reject) => { const chunkSize = 2097152; // Read in chunks of 2MB const spark = new SparkMD5.ArrayBuffer(); const fileReader = new FileReader(); let cursor = 0; // current cursor in file fileReader.onerror = function(): void { reject('MD5 computation failed - error reading the file'); }; // read chunk starting at `cursor` into memory function processChunk(chunk_start: number): void { const chunk_end = Math.min(file.size, chunk_start + chunkSize); fileReader.readAsArrayBuffer(file.slice(chunk_start, chunk_end)); } // when it's available in memory, process it // If using TS >= 3.6, you can use `FileReaderProgressEvent` type instead // of `any` for `e` variable, otherwise stick with `any` // See https://github.com/Microsoft/TypeScript/issues/25510 fileReader.onload = function(e: any): void { spark.append(e.target.result); // Accumulate chunk to md5 computation cursor += chunkSize; // Move past this chunk if (cursor < file.size) { // Enqueue next chunk to be accumulated processChunk(cursor); } else { // Computation ended, last chunk has been processed. Return as Promise value. // This returns the base64 encoded md5 hash, which is what // Rails ActiveStorage or cloud services expect resolve(btoa(spark.end(true))); // If you prefer the hexdigest form (looking like // '7cf530335b8547945f1a48880bc421b2'), replace the above line with: // resolve(spark.end()); } }; processChunk(0); }); } </code>
onFileSelected(event: any) {
    const file = event.target.files[0];
    if (file) {
      this.computeMD5(file);
    }
  }

  computeMD5(file: File) {
    const fileReader = new FileReader();
    fileReader.onload = (e) => {
      const arrayBuffer: ArrayBuffer = e.target?.result as ArrayBuffer;
      const md5 = SparkMD5.ArrayBuffer.hash(arrayBuffer);
      this.fileChecksum = md5;
    };
    fileReader.readAsArrayBuffer(file);
  }
  computeChecksumMd5(file: File): Promise<string> {
    return new Promise((resolve, reject) => {
      const chunkSize = 2097152; // Read in chunks of 2MB
      const spark = new SparkMD5.ArrayBuffer();
      const fileReader = new FileReader();
  
      let cursor = 0; // current cursor in file
  
      fileReader.onerror = function(): void {
        reject('MD5 computation failed - error reading the file');
      };
  
      // read chunk starting at `cursor` into memory
      function processChunk(chunk_start: number): void {
        const chunk_end = Math.min(file.size, chunk_start + chunkSize);
        fileReader.readAsArrayBuffer(file.slice(chunk_start, chunk_end));
      }
  
      // when it's available in memory, process it
      // If using TS >= 3.6, you can use `FileReaderProgressEvent` type instead 
      // of `any` for `e` variable, otherwise stick with `any`
      // See https://github.com/Microsoft/TypeScript/issues/25510
      fileReader.onload = function(e: any): void {
        spark.append(e.target.result); // Accumulate chunk to md5 computation
        cursor += chunkSize; // Move past this chunk
  
        if (cursor < file.size) {
          // Enqueue next chunk to be accumulated
          processChunk(cursor);
        } else {
          // Computation ended, last chunk has been processed. Return as Promise value.
          // This returns the base64 encoded md5 hash, which is what
          // Rails ActiveStorage or cloud services expect
          resolve(btoa(spark.end(true)));
  
          // If you prefer the hexdigest form (looking like
          // '7cf530335b8547945f1a48880bc421b2'), replace the above line with:
          // resolve(spark.end());
        }
      };
  
      processChunk(0);
    });
  }

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật