如何在PHP和python中执行校验和?

2024-07-05 14:50:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python代码,但是我需要用PHP做同样的事情,我不能

我必须在php中执行校验和,但是我没有做到这一点,在python中代码工作正常,但是在php中我没有做到这一点

这是python中的代码:

import hashlib
import sys

endUserId = "User123" #insert internal user ID here
applicationKey = "123456789123456789123456789" #insert application key here
applicationId = "1234" #insert App ID here.

checkSum = hashlib.md5(endUserId + applicationId + applicationKey)
userId = endUserId + "-" + applicationId + "-" + checkSum.hexdigest()[:10]
print(userId)

在PHP中,我只能创建MD5并选择前10个字符,但这对我没有帮助,因为我需要python中获得的校验和

<?php

$endUserId = "User123" //insert internal user ID here
$applicationKey = "123456789123456789123456789"; //insert application key here
$applicationId = "1234" //insert App ID here.

$hash = substr(md5($endUserId.'-'.$applicationId.'-'.$applicationKey), 0, 10);

$user_id = $endUserId.'-'.$applicationId.'-'.$hash;

<?

如何在php中执行校验和(checksum),就像上面的python代码一样,并获得10个字符


Tags: 代码importidhere校验hashlibinternalphp
1条回答
网友
1楼 · 发布于 2024-07-05 14:50:06

你可以这样做,我不知道你想要的是前10个还是后10个,但下面的代码将帮助你 如果前10个字符

    $result = mb_substr($user_id, 0, 10);

如果最后10个字符

    $result = mb_substr($user_id, -10);

确保使用if语句检查字符串的长度

    if(strlen($string) >= 10){
        //do awesome stuff
    }

相关问题 更多 >