import { md5, Message } from 'js-md5';

/**
 *
 * @param {any} str 字符串
 */
export const MD5 = (content: Message): string => {
  return md5(content);
};

export const fileMD5 = (file: File): Promise<string> => {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.onloadend = () => {
      const arrayBuffer = reader.result;
      if (arrayBuffer) {
        resolve(MD5(arrayBuffer));
      } else {
        resolve('');
      }
    };
    reader.onerror = (err) => {
      reject(err);
    };
    reader.readAsArrayBuffer(file);
  });
};
