import com.yutu.util.MessageUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
public class SHA256Test {
private static final String UTF8 = StandardCharsets.UTF_8.name();
/**
* 使用codec实现sha256编码
*/
@Test
public void S() throws Exception{
String str = "小汪学java";
System.out.println(DigestUtils.sha256Hex(str.getBytes(UTF8)));
//c115dd4fe1fbdb43cfc457e41e6de416d96d750b17afe95f44c8473ad1e5799d
}
/**
* 使用jdk原生的api实现sha256
* @throws Exception
*/
@Test
public void test1() throws Exception{
String str = "小汪学java";
String algorithm = "SHA-256";//算法名对象
String hexStr = MessageUtils.doDigest(str,algorithm);
System.out.println("hexStr: " + hexStr);
//hexStr: c115dd4fe1fbdb43cfc457e41e6de416d96d750b17afe95f44c8473ad1e5799d
}
}
11. SHA256编码
最新推荐文章于 2024-06-26 20:09:48 发布