目录

如何获取相册中文件的MD消息摘要值

问题现象

手机相册中文件的MD消息摘要值如何快速获取?

背景知识

解决方案

  1. 用户通过Picker拉起相册选择,选中目标文件后返回对应文件的Uri。
    export async function readUserPicFile(): Promise<Array<string>> {
      let uris: Array<string> = new Array<string>();
      try {
        let PhotoSelectOptions = new photoAccessHelper.PhotoSelectOptions();
        PhotoSelectOptions.maxSelectNumber = 5;
        PhotoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
        let photoPicker = new photoAccessHelper.PhotoViewPicker();
        await photoPicker.select(PhotoSelectOptions).then((PhotoSelectResult: photoAccessHelper.PhotoSelectResult) => {
          uris = PhotoSelectResult.photoUris;
        }).catch((err: BusinessError) => {
          console.error(`PhotoViewPicker.select failed with err: ${err.code}, ${err.message}`);
        });
      } catch (error) {
        let err = error as BusinessError;
        console.error(`DocumentViewPicker.select failed with err: ${err.message}`);
      }
      return uris;
    }
  2. 通过文件复制能力,将相册文件数据复制到沙箱文件中。
    // 复制文件到沙箱
    let file = fileIo.openSync(uri, fileIo.OpenMode.READ_ONLY);
    let pathDir = context.filesDir;
    let filePath = pathDir + '/' + file.name;
    fileIo.copyFileSync(file.fd, filePath);
  3. 通过cryptoFramework.createMd获取对应文件的MD消息摘要值。
    function fileMD5(filePath: string): string {
      // 定义摘要类型
      let md = cryptoFramework.createMd('SHA256');
      // 打开文件
      let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_ONLY);
      let fileBufferSize = 4096;
      let readSize = 0;
      let fileBuffer = new ArrayBuffer(fileBufferSize);
      let readOptions: ReadOptions = {
        offset: readSize,
        length: fileBufferSize
      };
      let readLength = fileIo.readSync(file.fd, fileBuffer, readOptions);
      while (readLength > 0) {
        // 更新摘要数据
        md.updateSync({ data: new Uint8Array(fileBuffer.slice(0, readLength)) });
        readSize += readLength;
        readOptions.offset = readSize;
        readLength = fileIo.readSync(file.fd, fileBuffer, readOptions);
      }
      // 计算摘要
      let mdResult = md.digestSync();
      return buffer.from(mdResult.data).toString('hex');
    }

完整示例参考如下:

import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { fileIo, hash, ReadOptions, } from '@kit.CoreFileKit';
import { common } from '@kit.AbilityKit';
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { buffer } from '@kit.ArkTS';

/**
 * 从媒体库选择图片资源
 */
export async function readUserPicFile(): Promise<Array<string>> {
  let uris: Array<string> = new Array<string>();
  try {
    let PhotoSelectOptions = new photoAccessHelper.PhotoSelectOptions();
    PhotoSelectOptions.maxSelectNumber = 5;
    PhotoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
    let photoPicker = new photoAccessHelper.PhotoViewPicker();
    await photoPicker.select(PhotoSelectOptions).then((PhotoSelectResult: photoAccessHelper.PhotoSelectResult) => {
      uris = PhotoSelectResult.photoUris;
    }).catch((err: BusinessError) => {
      console.error(`PhotoViewPicker.select failed with err: ${err.code}, ${err.message}`);
    });
  } catch (error) {
    let err = error as BusinessError;
    console.error(`DocumentViewPicker.select failed with err: ${err.message}`);
  }
  return uris;
}

@Entry
@Component
struct Index {
  @State message: string = '获取图片的消息摘要值';

  build() {
    RelativeContainer() {
      Text(this.message)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })
        .onClick(() => {
          let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
          readUserPicFile().then((uris: Array<string>) => {
            uris.forEach(async uri => {
              try {
                // 复制文件到沙箱
                let file = fileIo.openSync(uri, fileIo.OpenMode.READ_ONLY);
                let pathDir = context.filesDir;
                let filePath = pathDir + '/' + file.name;
                fileIo.copyFileSync(file.fd, filePath);
                await hash.hash(filePath, 'sha256').then((str: string) => {
                  console.info(`calculate file hash succeed: ${str}`);
                }).catch((err: BusinessError) => {
                  console.error(`calculate file hash failed with error message: ${err.message} , error code: ${err.code}`);
                });
                this.message = fileMD5(uri);
              } catch (e) {
                console.error(`failed with error message: ${e}`);
              }
            });
          });
        });
    }
    .height('100%')
    .width('100%');
  }
}

/**
 * 获取文件的MD5值
 */
function fileMD5(filePath: string): string {
  // 定义摘要类型
  let md = cryptoFramework.createMd('SHA256');
  // 打开文件
  let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_ONLY);
  let fileBufferSize = 4096;
  let readSize = 0;
  let fileBuffer = new ArrayBuffer(fileBufferSize);
  let readOptions: ReadOptions = {
    offset: readSize,
    length: fileBufferSize
  };
  let readLength = fileIo.readSync(file.fd, fileBuffer, readOptions);
  while (readLength > 0) {
    // 更新摘要数据
    md.updateSync({ data: new Uint8Array(fileBuffer.slice(0, readLength)) });
    readSize += readLength;
    readOptions.offset = readSize;
    readLength = fileIo.readSync(file.fd, fileBuffer, readOptions);
  }
  // 计算摘要
  let mdResult = md.digestSync();
  return buffer.from(mdResult.data).toString('hex');
}