<?php
namespace Payment\Utils; class StrUtil { public static function getNonceStr($length = 32) { $chars = "abcdefghijklmnopqrstuvwxyz0123456789"; $str = ""; for ($i = 0; $i < $length; $i++) { $str .= substr($chars, mt_rand(0, strlen($chars)-1), 1); } return $str; } public static function characet($str, $targetCharset) { if (empty($str)) { return $str; } if (strcasecmp('UTF-8', $targetCharset) != 0) { $str = mb_convert_encoding($str, $targetCharset, 'UTF-8'); } return $str; } public static function String2Hex($string) { $hex = ''; $len = strlen($string); for ($i=0; $i < $len; $i++) { $hex .= dechex(ord($string[$i])); } return $hex; } public static function getRsaKeyValue($key, $type = 'private') { $keyStr = $key; if (empty($keyStr)) { return null; } $keyStr = str_replace(PHP_EOL, '', $keyStr); if ($type === 'private') { $beginStr = '-----BEGIN RSA PRIVATE KEY-----'; $endStr = '-----END RSA PRIVATE KEY-----'; } else { $beginStr = '-----BEGIN PUBLIC KEY-----'; $endStr = '-----END PUBLIC KEY-----'; } $rsaKey = chunk_split(base64_encode($keyStr), 64, "\n"); $rsaKey = $beginStr . PHP_EOL . $keyStr . PHP_EOL . $endStr; return $rsaKey; } } 