MessageDigest的更新方法是什么,BASE64Encoder是什么意思? [英] What does update method of MessageDigest do and what is BASE64Encoder meant for?

查看:1223
本文介绍了MessageDigest的更新方法是什么,BASE64Encoder是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是将加密用户String的代码:

Following is a code that will encrypts the user String :

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import sun.misc.BASE64Encoder;
import java.io.*;

class Encrypter {
public synchronized String encrypt(String plainText) throws Exception {
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("SHA");
    }catch(Exception exc) {
        throw new Exception(exc.getMessage());
     }

     try {
        md.update(plainText.getBytes("UTF-8"));
     }catch(Exception exc) {
        throw new Exception(exc.getMessage());
      }

      byte raw[] = md.digest();
      String hash = (new BASE64Encoder()).encode(raw);
      return hash;
}
public static void main(String args[]) {
    try {
        Encrypter encrypter = new Encrypter();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String userInput = br.readLine();
        String encryptedPassword = encrypter.encrypt(userInput);
        System.out.println(encryptedPassword);
    } catch(Exception exc) {
        System.out.println(exc);
      }
}
}

得到以下警告:

Encrypter.java:4: warning: BASE64Encoder is internal proprietary API and may be removed in a future release
import sun.misc.BASE64Encoder;
           ^
Encrypter.java:23: warning: BASE64Encoder is internal proprietary API and may be removed in a future release
              String hash = (new BASE64Encoder()).encode(raw);
                                 ^
2 warnings

更新 c> MessageDigest do?即语句 md.update(plainText.getBytes(UTF-8)); do?

What does the method update of class MessageDigest do ? i.e What does the statement md.update(plainText.getBytes("UTF-8")); do ?

什么是 BASE64Encoder 类?我找不到它的DOC

What is a BASE64Encoder class ? I couldn't find it's DOC

推荐答案


  1. 您没有执行任何加密。您正在计算一个单向哈希或输入的摘要。此哈希可以稍后用于验证消息的完整性。请参见散列 SHA1 < a>和 MessageDigest

Base64编码是一种表示二进制数据以ASCII。这通常是所期望的,因为不是所有的数据存储和传输机制都支持原始二进制。例如,如果要通过http查询字符串参数传输计算摘要,则需要将其编码为Base64。此外,保存或打印原始二进制到控制台将产生一串可能在可打印范围之外的时髦字符,也可能从您的PC扬声器产生蜂鸣声!

Base64 encoding is a method of representing binary data in ASCII. This is often desirable because not all data storage and transmission mechanisms support raw binary. For example, if you want to transfer your computed digest via an http query string parameter, you'll want to encode it as Base64. Also, saving or printing raw binary to the console will produce a stream of funky characters which may be outside of the printable range, and may also produce beeps from your PC speaker!

您使用的 Base64Encoder 来自 sun.misc 包,不应该使用。这是内部Sun JVM代码,将来可能会或可能不会提供。

The Base64Encoder you're using comes from the sun.misc package and should NEVER be used. This is internal Sun JVM code which may or may not be available in the future. That also explains why you're weren't able to find any javadoc.

幸运的是,存在几个自由和开放的Base64编码器和解码器。 Apache Commons Codec 是一种广泛使用且稳定的库,其中包含多个编解码器,包括 Base64

Fortunately, several free and open Base64 encoders and decoders exist. Apache Commons Codec is a widely used and stable library which contains several codecs include Base64.

md.update(plainText.getBytes(UTF-8))摘要。调用 digest 执行最终更新并计算输入的摘要。请参见 md.digest md.update

md.update(plainText.getBytes("UTF-8")) updates the input to the digest. Calling digest performs a final update and computes the digest of the input. See javadoc of md.digest and md.update

这篇关于MessageDigest的更新方法是什么,BASE64Encoder是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆