1

我正在尝试改变这个专有的

private String getSecWebSocketAccept(String secKey) 
    throws UnsupportedEncodingException, NoSuchAlgorithmException {
    String guid = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
    secKey += guid;
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    md.update(secKey.getBytes("ISO-8859-1"), 0, secKey.length());
    byte[] shalHash = md.digest();
    BASE64Encoder encoder = new BASE64Encoder();
    return encoder.encode(shalHash);
}

编码为 apache 版本

org.apache.commons.codec.binary.Base64

但是当我尝试时它根本行不通

private String getSecWebSocketAccept(String secKey) 
    throws UnsupportedEncodingException, NoSuchAlgorithmException {
    String guid = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
    secKey += guid;
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    md.update(secKey.getBytes("ISO-8859-1"), 0, secKey.length());
    byte[] shalHash = md.digest();
    org.apache.commons.codec.binary.Base64.encodeBase64 encoder = new org.apache.commons.codec.binary.Base64.encodeBase64();
    return encoder.encode(shalHash);
}

这是我得到的错误

C:\xampp\htdocs\html>javac SimpleServer.java
SimpleServer.java:87: error: cannot find symbol
                org.apache.commons.codec.binary.Base64.encodeBase64 encoder = ne
w org.apache.commons.codec.binary.Base64.encodeBase64();
                                                      ^
  symbol:   class encodeBase64
  location: class Base64
SimpleServer.java:87: error: cannot find symbol
                org.apache.commons.codec.binary.Base64.encodeBase64 encoder = ne
w org.apache.commons.codec.binary.Base64.encodeBase64();

                                        ^
  symbol:   class encodeBase64
  location: class Base64
2 errors
4

1 回答 1

3

org.apache.commons.codec.binary.Base64.encodeBase64是一种静态方法。像这样使用它:

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

此外,您可以使用org.apache.commons.codec.digest.DigestUtils.sha1(byte[] data)

byte[] shalHash = DigestUtil.sha1(secKey.getBytes("ISO-8859-1"));
于 2013-06-05T09:25:09.490 回答