java sha1 base64_在java util类中使用BASE64的SHA1不会生成正确的密码

import java.io.UnsupportedEncodingException;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

import java.util.Calendar;

import java.util.Date;

import java.util.GregorianCalendar;

import java.util.TimeZone;

import java.util.UUID;

import javax.xml.bind.DatatypeConverter;

import org.apache.commons.codec.binary.Base64;

public class OminitureWSUtil {

private static MessageDigest SHA1;

static {

try {

SHA1 = MessageDigest.getInstance("SHA1");

} catch(NoSuchAlgorithmException nae) {

throw new RuntimeException(nae);

}

}

static class OmniturePasswordDigest {

private final String timestamp;

private final String nonce;

private final String secret;

private String password;

public OmniturePasswordDigest(String secret) {

Calendar c = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT+0"));

c.setTime(new Date());

//timestamp = DatatypeConverter.printDateTime(c);

//nonce = UUID.randomUUID().toString().replace("-", "");

timestamp = "2011-01-26T20:10:56Z";

nonce = "MTkyMTYwZWMzMjIzZGJmYzNiYmE5M2E5";

this.secret = secret;

}

public String getTimestamp() {

return timestamp;

}

public String getNonce() {

return nonce;

}

public String generatePassword() {

if(password == null) {

String beforeEncryption = nonce+timestamp+secret;

System.out.println("before encryption, encoding: " + beforeEncryption);

try {

SHA1.reset();

byte[] toEncrypt = beforeEncryption.getBytes("UTF-8");

//SHA1.update(toEncrypt, 0, toEncrypt.length);

SHA1.update(beforeEncryption.getBytes());

} catch (UnsupportedEncodingException uee) {

throw new RuntimeException(uee);

}

byte[] encryptedRaw = SHA1.digest();

byte[] encoded = Base64.encodeBase64(encryptedRaw);

try {

password = new String(encoded, "UTF-8");

} catch (UnsupportedEncodingException uee) {

throw new RuntimeException(uee);

}

}

return password;

}

}

public static OmniturePasswordDigest generatePasswordDigest(String secret) {

return new OmniturePasswordDigest(secret);

}

public static void main(String[] args) {

OmniturePasswordDigest opd = generatePasswordDigest("1779ab07fb93a01e3d4a6ee174124b91");

System.out.println("nonce: " + opd.getNonce());

System.out.println("timestamp: " + opd.getTimestamp());

System.out.println("password: " + opd.generatePassword());

if("Lr+m+/6y3XUxvjd8Rtn75gqn/b4=".equals(opd.generatePassword())) {

System.out.println("all good");

} else {

System.out.println("generated password is not the same, it should be: " +

"Lr+m+/6y3XUxvjd8Rtn75gqn/b4=");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值