如何从签名字符串生成哈希值?

时间:2016-02-17 06:53:19

标签: php hmacsha1

我正在尝试为我的本地提供程序使用api访问,因为api文档说我必须使用哈希值将post的参数发送到api gate方式。 Api门方式返回010错误,当我向他们邮寄该错误时他们说哈希值错误。正如api文档所说我必须通过ascii对post中的所有输入参数进行排序以创建签名字符串,然后通过用户密钥将hash_mac转换为签名字符串。我很困惑“用ascii排序所有输入参数”是什么意思。非常感谢任何解释。

文件说 - 哈希值由HMACSHA1使用提供商提供的密钥计算。 HashValue将通过以下方法生成: 1.所有由客户填写的输入参数按ASCII排序为签名字符串。 2.签名字符串将由HMACSHA1加密,并提供密钥 由提供者。

用于生成哈希值的Api示例代码

<?php
$signData = hash_hmac('sha1', 
SignatureString,Secret Key, false);
$signData= strtoupper($signData);
echo urlencode($signData);
?> 

用户输入参数

<form action="http://121.143.168.11/Payment/Payment/pay" method="post">
  <input type="hidden" id="merchantID" name="merchantID" value="382987634198262"/>
  <input type="hidden" id="invoiceNo" name="invoiceNo" value="0345"/>
  <input type="hidden" id="productDesc" name="productDesc" value="TESTProduct"/>
  <input type="hidden" id="amount" name="amount" value="000000010000"/>
  <input type="hidden" id="currencyCode" name="currencyCode" value="104"/>
  <input type="hidden" id="categoryCode" name="categoryCode" value="000012"/>
  <input type="hidden" id="userDefined1" name="userDefined1" value="UserDefined1"/>
  <input type="hidden" id="userDefined2" name="userDefined2" value="UserDefined2"/>
  <input type="hidden" id="userDefined3" name="userDefined3" value="UserDefined3"/>
  <input type="hidden" id="hashValue" name="hashValue" value="BA115F06B0629EC69EDBA33604301B891D5C9F8C"/>
/>

我的问题是如何为上述用户输入参数生成签名字符串。

由于

2 个答案:

答案 0 :(得分:0)

假设您通过POST提交此表单,并且POST变量不包含任何垃圾,并且我正确理解“sorted by”指令:

public class CustomAdapterPooja extends BaseAdapter {

        private  LayoutInflater inflater=null;
         Context context;
        private ArrayList<HashMap<String,String>> listData;
        private static final String TAG_POOJA_NAME="pooja_name";
        private static final String TAG_POOJA_AMOUNT="amount";
        private static final String TAG_POOJA_DAYS="worship";
        Calendar newCalendar = Calendar.getInstance();

        public CustomAdapterPooja(FragmentActivity mainActivity, ArrayList<HashMap<String,String>> listData) {
            // TODO Auto-generated constructor stub

            context = mainActivity;
            this.listData=listData;
            inflater = ( LayoutInflater )context.
                    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return listData.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public class Holder
        {
            TextView tv;
            TextView serviceprice;

            public CheckBox checks;
            public TextView dayss;
            public ImageView seledates;
            public TextView txtseledates;
        }
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            final Holder holder=new Holder();
            String rup="\u20B9";
            View rowView;
            rowView = inflater.inflate(R.layout.list_item_poojaselection, null);
            holder.tv=(TextView) rowView.findViewById(R.id.list_item_poojaname);
            holder.serviceprice=(TextView)rowView.findViewById(R.id.list_item_poojaprice);
            holder.dayss=(TextView)rowView.findViewById(R.id.list_item_poojadays);
            holder.seledates=(ImageView)rowView.findViewById(R.id.list_item_selectdates);
            holder.txtseledates=(TextView)rowView.findViewById(R.id.selecteddatess);


            holder.checks=(CheckBox)rowView.findViewById(R.id.list_item_poojacheck);

            holder.checks.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    System.out.println("hello"+listData.get(position).get(TAG_POOJA_AMOUNT));
                }
            });

            holder.seledates.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {



                    datePickerDialog.show();


                }
            });
            holder.tv.setText(listData.get(position).get(TAG_POOJA_NAME));
            holder.dayss.setText(listData.get(position).get(TAG_POOJA_DAYS));
            holder.serviceprice.setText(rup + listData.get(position).get(TAG_POOJA_AMOUNT));

            datePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    sYear = year;
                    sMonth = monthOfYear;
                    sDate = dayOfMonth;
                    holder.txtseledates.setText(new StringBuilder()
                            // Month is 0 based, just add 1
                            .append(sYear).append("-").append(sMonth + 1).append("-").append(sDate));
                /*selecdts=textcal.getText().toString();
                System.out.println("Selected dtes"+selecdts);*/
                }
            }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
            datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
            return rowView;
        }

    }

无需向$secret = 'XXXXXX'; ksort($_POST); // sort by input names alphabetically $signature_source = ''; foreach ($_POST as $value) { $signature_source .= $value; } $signature = hash_hmac('sha1', $signature_source, $secret, false); 申请,除非图片中还有其他内容我缺席。

答案 1 :(得分:0)

$_form_data = array(
    'merchantID' => '400123456712345',
    'invoiceNo' => '1234567890333',
    'productDesc' => 'Test Product',
    'amount' => '000000010000',
    'currencyCode' => 840,
    'userDefined1' => 'userDefined1',
    'userDefined2' => 'userDefined2',
    'userDefined3' => 'userDefined3',
);

function getPaymentHash($data)
{
    asort($data, SORT_STRING); // sorting values by ASCII
    $str = implode('', $data);
    $signData = hash_hmac('sha1', $str, 'secret key', false);
    $signData = strtoupper($signData);
    return urlencode($signData);
}

$hashValue = getPaymentHash($_form_data);
祝你好运:)