<?php

namespace common\pay\renren;

class PayUtils
{
    /**
     * @param array $data
     * @param string $secretKey
     * @return string
     */
    public static function makeSign($data, $secretKey)
    {
        $str = '';
        ksort($data);
        foreach ($data as $key => $value) {
            if ($key == 'Sign') {
                continue;
            }
            $value = strval($value);
            if ($value === '') {
                continue;
            }
            $str .= "{$key}={$value}&";
        }
        $str = rtrim($str, '&') . "&Key={$secretKey}";
        return strtoupper(md5($str));
    }

    /**
     * @param array $data  参数
     * @param string $secretKey
     * @return bool
     */
    public static function checkSign($data, $secretKey)
    {
        $sign = isset($data['Sign']) ? $data['Sign'] : null;
        if ($sign == null) {
            return false;
        }
        return self::makeSign($data, $secretKey) === $sign;
    }
}