duanqiao1961 2015-06-09 10:38
浏览 160
已采纳

PHP base64编码丢失了错误的答案

I'm currently working on a website, which must implement a web-socket server. I am using PHP to make the server. I extracted a Sec-websocket-key from the clients response, then I hashed it using the PHP sha1() command.

The problem is that when I want to base64_encode it then it gives an incorrect accept key which is an error. Was there a change in base64_encode command?

Thank you for the answers!

This is just a test code, it doesn't allow for multiple clients:

$address = "127.0.0.1";
$port = 9000;

$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($sock, $address, $port);
socket_listen($sock);

$client = socket_accept($sock);

$handshake = Array();

$response = socket_read($client, 1024);

$handshake = split("
", $response);

$socketKey = split(" ", $handshake[11]);

$rawAccept =  socketKey[1] . "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";

$rawAccept = str_replace(" ", "", $rawAccept);

$rawSha1Accept = sha1($rawAccept);

$accept = base64_encode($rawSha1Accept);

$upgrade = "HTTP/1.1 101 Switching Protocols
" .
            "Upgrade: websocket
" .
            "Connection: Upgrade
" .
            "Sec-WebSocket-Accept: $accept

";

socket_write($client, $upgrade, 1024);
  • 写回答

2条回答 默认 最新

  • duanli0453 2015-06-19 13:27
    关注

    The function sha1() will already encode the hash value, so you should probably calculate the binary hash before encoding:

    $rawSha1Accept = sha1($rawAccept, true); // Note the second parameter
    $accept = base64_encode($rawSha1Accept);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加