軽く SHA1 ハッシュするかー、どうせまた使うだろうから Util クラス作るかーと思って実装方法調べるとアルゴリズム指定する所で文字列で指定するみたい。
このへん決まりきっているんだから定数欲しいよなー
もう作ってる人いるんじゃないのかー?と調べてみたら
Apache Commons Codec があったので使ってみた。
そしたら DigestUtils でエラーが出たので調べると StackOverflow の質問があった。
apache - Method not found using DigestUtils in Android - Stack Overflow
eclipse - NoSuchMethodError using commonc codec in Android application - Stack Overflow
読むのが面倒なのでコピーしてメソッド削ってメソッド名変えて
MessageDigest ってあまり馴染みがないのでクラス名を HashUtils にした。
add HashUtils · 6bb32f1 · wada811/AndroidLibrary-wada811
codec.StringUtils はクラス名衝突が起きる可能性が高かったので EncodingUtils にしておいた。
/*
* Copyright 2013 wada811<at.wada811@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package at.wada811.utils;
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import at.wada811.codec.EncodingUtils;
import at.wada811.codec.Hex;
import at.wada811.codec.MessageDigestAlgorithms;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class HashUtils {
private static MessageDigest getDigest(final String algorithm){
try{
return MessageDigest.getInstance(algorithm);
}catch(final NoSuchAlgorithmException e){
throw new IllegalArgumentException(e);
}
}
private static MessageDigest getMd5Digest(){
return getDigest(MessageDigestAlgorithms.MD5);
}
private static MessageDigest getSha1Digest(){
return getDigest(MessageDigestAlgorithms.SHA_1);
}
private static MessageDigest getSha256Digest(){
return getDigest(MessageDigestAlgorithms.SHA_256);
}
private static MessageDigest getSha512Digest(){
return getDigest(MessageDigestAlgorithms.SHA_512);
}
public static byte[] md5(final byte[] data){
return getMd5Digest().digest(data);
}
public static byte[] md5(final String data){
return md5(EncodingUtils.getBytesUtf8(data));
}
public static String toMD5(final byte[] data){
return Hex.encodeHexString(md5(data));
}
public static String toMD5(final String data){
return Hex.encodeHexString(md5(data));
}
public static byte[] sha1(final byte[] data){
return getSha1Digest().digest(data);
}
public static byte[] sha1(final String data){
return sha1(EncodingUtils.getBytesUtf8(data));
}
public static String toSHA1(final byte[] data){
return Hex.encodeHexString(sha1(data));
}
public static String toSHA1(final String data){
return Hex.encodeHexString(sha1(data));
}
public static byte[] sha256(final byte[] data){
return getSha256Digest().digest(data);
}
public static byte[] sha256(final String data){
return sha256(EncodingUtils.getBytesUtf8(data));
}
public static String toSHA256(final byte[] data){
return Hex.encodeHexString(sha256(data));
}
public static String toSHA256(final String data){
return Hex.encodeHexString(sha256(data));
}
public static byte[] sha512(final byte[] data){
return getSha512Digest().digest(data);
}
public static byte[] sha512(final String data){
return sha512(EncodingUtils.getBytesUtf8(data));
}
public static String toSHA512(final byte[] data){
return Hex.encodeHexString(sha512(data));
}
public static String toSHA512(final String data){
return Hex.encodeHexString(sha512(data));
}
}byte[] を使うか返すかするメソッド使うかなぁ?使用タイミングがわからないけど一応残しておいた。