解码base64和消息摘要SHA1的Groovy脚本。无法运行脚本

时间:2018-01-25 13:06:12

标签: groovy

我是 Groovy脚本的新手。我使用脚本在字符串上执行decodebase64并对数据执行Messagedigest SHA1 。我设法得到了以下代码,但它无法正常工作。

import java.security.*;
def Message processData(Message message) {
    //Body 
    def body = message.getBody(String.class);
    String decoded = body.bytes.decodeBase64().toVarchar();
    MessageDigest sha1 = MessageDigest.getInstance("SHA1");
    byte[] digest  = sha1.digest(decode.getBytes());    
    digest = new BigInteger(1, digest).toString(16);
    message.setBody(digest);
    return message;
}

输入将是:

  

kIAUQJVfhfLaTF2G4WouvCmZqA2zhFhujfqe3g1I4WBJobFrBWcXKG5XoPM1MQZF9UeoM0o99tf8FHVEkEIkN1s0Pw7vXyHurkbiY / Dic56Fqa + PovU9gU4US + oLHoEMSpoDMtrVtAskS0h6bCgv1Hph / 1QHTlVeto / GIw0Obb9CtSqlfhoXL8ZqCBMlsWClF8q31PZaD4vt8ZRzjm / fnA1YsH8Fo5PfxdWFEVrsprvDyUHuRvr0CHbDyE7qszgYA + Ri0ylnulPEqOlsBvNpiXoiEtoOnFOjZrWmLCM6IjnR8b2efnTRQsELdYr3FjRbg1kLDz3xUDGBwTplF31CSw ==

解码后,输入中包含unicode characters。我无法接受它。在SHA1之后,输出将为40个字符。

你能帮忙吗?

1 个答案:

答案 0 :(得分:0)

不确定toVarchar()正在做什么,但我怀疑它有错...

import java.security.MessageDigest

def msg = 'kIAUQJVfhfLaTF2G4WouvCmZqA2zhFhujfqe3g1I4WBJobFrBWcXKG5XoPM1MQZF9UeoM0o99tf8FHVEkEIkN1s0Pw7vXyHurkbiY/Dic56Fqa+PovU9gU4US+oLHoEMSpoDMtrVtAskS0h6bCgv1Hph/1QHTlVeto/GIw0Obb9CtSqlfhoXL8ZqCBMlsWClF8q31PZaD4vt8ZRzjm/fnA1YsH8Fo5PfxdWFEVrsprvDyUHuRvr0CHbDyE7qszgYA+Ri0ylnulPEqOlsBvNpiXoiEtoOnFOjZrWmLCM6IjnR8b2efnTRQsELdYr3FjRbg1kLDz3xUDGBwTplF31CSw=='

def digest = MessageDigest.getInstance('SHA1').digest(msg.decodeBase64())
def result = new BigInteger(1, digest).toString(16)

println result

给我

17a88df21eb9ad8669864f3ccdd435bfc1e054aa