Created
August 31, 2012 03:36
-
-
Save kojiokb/3548765 to your computer and use it in GitHub Desktop.
MessageDigest メモ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.util.Log; | |
public class MainActivity extends Activity { | |
private final String TAG = getClass().getSimpleName(); | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
String message = "Android"; | |
/* | |
* MD5 : 128bit | |
* SHA-1 : 160bit | |
* SHA-224 : 224bit | |
* SHA-256 : 256bit | |
* SHA-384 : 384bit | |
* SHA-512 : 512bit | |
* メモ : SHA-224〜SHA-512を総称してSHA-2と呼ぶ | |
* SHAはSHA-1と同義 | |
* SHA-224は非サポート | |
*/ | |
String[] algorithm = { | |
"MD5", "SHA", "SHA-1", "SHA-224", "SHA-256", "SHA-384", "SHA-512" | |
}; | |
for (int i = 0; i < algorithm.length; i++) { | |
try { | |
StringBuffer digest = new StringBuffer(); | |
MessageDigest messageDigest = MessageDigest.getInstance(algorithm[i]); | |
messageDigest.update(message.getBytes()); | |
byte digestByteArray[] = messageDigest.digest(); | |
for (int j = 0; j < digestByteArray.length; j++) { | |
digest.append(Integer.toHexString(0xFF & digestByteArray[j])); | |
} | |
Log.e(TAG, "Digest(" + algorithm[i] + ") : " + digest); | |
} catch (NoSuchAlgorithmException e) { | |
Log.e(TAG, "NoSuchAlgorithmException : " + e); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment