'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var nodejs_crypto = require('../../nodejs/crypto.js');

/**
 * 密码学随机数
 * @private
 */
var randomNum = function randomNum(crypto) {
  var buf = crypto.randomBytes(6);
  var hexToDec = parseInt(buf.toString('hex'), 16);
  return hexToDec;
};
var sha256 = function sha256(secret, content, crypto) {
  return crypto.createHmac('sha256', secret).update(content).digest('base64');
};
var sha1 = function sha1(secret, content, crypto) {
  return crypto.createHmac('sha1', secret).update(content).digest('base64');
};
/**
 * 获取API算法签名，默认sha1
 * @private
 * @param secret
 * @param content
 * @param signMethod
 */
var signatureGenerator = function signatureGenerator(secret, content, signMethod, crypto) {
  switch (signMethod) {
    case 'sha256':
      return sha256(secret, content, crypto);
    case 'sha1':
    default:
      return sha1(secret, content, crypto);
  }
};
/**
 * 请求签名Header生成
 * @private
 * @param {object} signInfo 密钥信息
 * @example
 * ```ts
 * const headers = genRainbowHeaderSignature({
 *   appId: 'xxx',
 *   userId: 'yyy',
 *   secretKey: 'zzz',
 *   signMethod: 'sha1',
 * });
 * // {
 * //   rainbow_sgn_type: 'apisign',
 * //   rainbow_version: '2020',
 * //   rainbow_app_id: 'xxx',
 * //   rainbow_user_id: 'yyy',
 * //   rainbow_timestamp: '1700000000',
 * //   rainbow_nonce: '...',
 * //   rainbow_sgn_method: 'sha1',
 * //   rainbow_sgn_body: '',
 * //   rainbow_signature: '...',
 * // }
 * ```
 */
function genRainbowHeaderSignature(signInfo) {
  var crypto = nodejs_crypto.getCrypto();
  var appID = signInfo.appID,
    userID = signInfo.userID,
    appId = signInfo.appId,
    userId = signInfo.userId,
    _signInfo$signMethod = signInfo.signMethod,
    signMethod = _signInfo$signMethod === void 0 ? 'sha1' : _signInfo$signMethod,
    secretKey = signInfo.secretKey;
  if (!secretKey) {
    return {};
  }
  var rainbowSgnType = 'apisign';
  var rainbowVersion = '2020';
  var rainbowAppId = appId || appID;
  var rainbowUserId = userId || userID;
  var rainbowTimestamp = "".concat(Math.ceil(new Date().getTime() / 1000)); // ms => s
  var rainbowNonce = "".concat(randomNum(crypto));
  var rainbowSgnMethod = signMethod;
  var rainbowSgnBody = '';
  var content = "".concat(rainbowVersion, ".").concat(rainbowAppId, ".").concat(rainbowUserId, ".").concat(rainbowTimestamp, ".").concat(rainbowNonce) + ".".concat(rainbowSgnMethod, ".").concat(rainbowSgnBody);
  var rainbowSignature = signatureGenerator(secretKey, content, signMethod, crypto);
  return {
    rainbow_sgn_type: rainbowSgnType,
    rainbow_version: rainbowVersion,
    rainbow_app_id: rainbowAppId,
    rainbow_user_id: rainbowUserId,
    rainbow_timestamp: rainbowTimestamp,
    rainbow_nonce: rainbowNonce,
    rainbow_sgn_method: rainbowSgnMethod,
    rainbow_sgn_body: rainbowSgnBody,
    rainbow_signature: rainbowSignature // 签名串	如%2BysXvBSshSbHOsCX2zWBE1tapVs68hi5GLdcQtwBUNk%3D
  };
}

exports.genRainbowHeaderSignature = genRainbowHeaderSignature;
