Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Python中实现将数据编码为MD5字符串的算法?_Java_Python - Fatal编程技术网

Java 如何在Python中实现将数据编码为MD5字符串的算法?

Java 如何在Python中实现将数据编码为MD5字符串的算法?,java,python,Java,Python,我不熟悉Python。我有一个java代码段来实现将数据编码为MD5字符串的算法。我想把它转换成Python。我的java代码使用commons-codec1.9对Base64进行编码。 有人能帮忙吗?非常感谢 public static void Main(args){ String data ="token=cd264af2fbd&app_key=060176144a&client_os_type=4"; String key = "87483ik98abh"; St

我不熟悉Python。我有一个java代码段来实现将数据编码为MD5字符串的算法。我想把它转换成Python。我的java代码使用commons-codec1.9对Base64进行编码。 有人能帮忙吗?非常感谢

 public static void Main(args){
 String data ="token=cd264af2fbd&app_key=060176144a&client_os_type=4";
 String key = "87483ik98abh";
 String md5EncodedData = getEncodeData(data, key);
}


您看过python的hashlib模块了吗


如果您想使用python高效地编写MD5哈希代码,我的github存储库肯定会有助于理解和解释python中MD%的数学定义

```

def md5(消息):

message=bytearray(message)#将输入复制到可变缓冲区中
原始长度单位为位=(8*len(消息))&0xffffffffffffff
message.append(0x80)
而len(消息)%64!=56:
message.append(0)
消息+=原始长度(以位为单位)到字节(8,byteorder='little')
哈希值=初始值[:]
对于范围(0,len(message),64)内的数据块:
a、 b,c,d=散列块
chunk=消息[chunk_of st:chunk_of st+64]
对于范围(64)内的i:
f=功能[i](b、c、d)
g=索引函数[i](i)
to_rotate=a+f+常量[i]+int.from_字节(chunk[4*g:4*g+4],byteorder='little')
新的b=(b+左旋转(旋转,旋转数量[i])和0xFFFFFF
a、 b,c,d=d,新的
对于枚举中的i,val([a,b,c,d]):
散列块[i]+=val
散列块[i]&=0xFFFFFFFF
返回和(xIt)可能有帮助:
private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";
public String getEncodeData(String app_secret, String data){
    try{
        byte[] hmc = calculateRFC2104HMAC(Base64.encodeBase64(data.getBytes()), app_secret);
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] thedigest = md.digest(hmc);
        return new String(Hex.encodeHex(thedigest));

    }catch (Exception ex){
        System.out.print(ex.getMessage().toString());
    }
    return "";
}
public static byte[] calculateRFC2104HMAC(byte[] data, String key)
        throws java.security.SignatureException
{
    try {

        // get an hmac_sha1 key from the raw key bytes
        SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);

        // get an hmac_sha1 Mac instance and initialize with the signing key
        Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
        mac.init(signingKey);

        // compute the hmac on input data bytes
        byte[] rawHmac = mac.doFinal(data);
        return rawHmac;

    } catch (Exception e) {
        throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
    }
}
message = bytearray(message) #copy our input into a mutable buffer
orig_len_in_bits = (8 * len(message)) & 0xffffffffffffffff
message.append(0x80)
while len(message)%64 != 56:
    message.append(0)
message += orig_len_in_bits.to_bytes(8, byteorder='little')

hash_pieces = init_values[:]

for chunk_ofst in range(0, len(message), 64):
    a, b, c, d = hash_pieces
    chunk = message[chunk_ofst:chunk_ofst+64]
    for i in range(64):
        f = functions[i](b, c, d)
        g = index_functions[i](i)
        to_rotate = a + f + constants[i] + int.from_bytes(chunk[4*g:4*g+4], byteorder='little')
        new_b = (b + left_rotate(to_rotate, rotate_amounts[i])) & 0xFFFFFFFF
        a, b, c, d = d, new_b, b, c
    for i, val in enumerate([a, b, c, d]):
        hash_pieces[i] += val
        hash_pieces[i] &= 0xFFFFFFFF

return sum(x<<(32*i) for i, x in enumerate(hash_pieces)