import { UUID, Store, MD5, DES } from 'amos-tool';

const LEN = {
  small: 16,
  large: 32
};


/**
 * 普通密码生成策略
 * pwd + secretKey => base64 => pwd
 * @param {string} password
 * @return {object} { password, secretKey }
 */
export const normalPolicy = function(password = '') {
  const secretKey = UUID.uuid(LEN.small);
  password += secretKey;
  const p = Store.encrypt(password);

  return {
    password: p,
    secretKey
  };
};

/**
 * 普通密码生成策略
 * @param {string} password 密码
 * @param {string} secretKey 秘钥
 * @return {object} { password, secretKey }
 */
export const advancePolicy = function(password = '', secretKey) {
  secretKey || (secretKey = UUID.uuid(LEN.large));
  password += secretKey;
  const p = Store.encrypt(password);

  return {
    password: p,
    secretKey
  };
};

/**
 * 采用MD5生成密码
 * @param {string} password
 * @param {string} secretKey
 */
export const useMd5Policy = function(password = '', secretKey) {
  secretKey || (secretKey = UUID.uuid(LEN.large));
  const md5pwd = MD5(password, secretKey);

  return {
    password: md5pwd,
    secretKey
  };
};

/**
 * 采用MD5生成密码
 * 1. password = md5(password);
 * 2. password = Store.encrypt(password + secretKey);
 *
 * @param {string} password
 * @param {string} secretKey
 */
export const useMd5Policy2 = function(password, secretKey) {
  password || (password = '');
  secretKey || (secretKey = UUID.uuid(LEN.large));
  let md5pwd = MD5(password);
  md5pwd += secretKey;
  const p = Store.encrypt(md5pwd);

  return {
    password: p,
    secretKey
  };
};

/**
 * 采用DES生成密码
 * 1. password = DES.encode(password, secretKey);
 *
 * @param {string} password
 * @param {string} secretKey
 */
export const useDESPolicy = function(password = '', secretKey) {
  secretKey || (secretKey = UUID.uuid(LEN.large));
  const desPpwd = DES.encode(password, secretKey);
  return {
    password: desPpwd,
    secretKey
  };
};
