Haloo semua,
Sudah lumayan lama saya tidak memposting, kali ini saya akan membagikan cara membuat md5 pada Android.
pertama buat activity_main.xml untuk view nya :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#b331b6e7" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <EditText android:id="@+id/edittext1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="14dp" android:background="@drawable/abs__textfield_search_default_holo_light" android:hint="@string/value_hint" android:padding="5dp" android:singleLine="true" android:textColor="#f0f0f0" android:textColorHint="#f0f0f0" android:textSize="16sp" /> <TextView android:id="@+id/textview2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/edittext1" android:layout_centerHorizontal="true" android:layout_marginTop="18dp" android:gravity="center" android:padding="5dp" android:text="@string/value_md5" android:textColor="#ffffff" android:textSize="13sp" android:textStyle="bold" /> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/textview2" android:layout_marginTop="20dp" android:background="#039bcf" android:padding="10dp" android:text="@string/button1" android:textColor="#f0f0f0" android:textSize="15sp" /> <TextView android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:gravity="center" android:padding="5dp" android:text="@string/footer" android:textColor="#ffffff" android:textSize="12sp" /> </RelativeLayout>
sekarang buat MainActivity.java :
package com.kreativdeveloper.md5; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { private TextView mTextView; private EditText mEdittext; private Button mButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mEdittext = (EditText) findViewById(R.id.edittext1); mTextView = (TextView) findViewById(R.id.textview2); mButton = (Button) findViewById(R.id.button1); mButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { computeMD5(); } }); } private void computeMD5() { String password = mEdittext.getText().toString(); try { MessageDigest digest = java.security.MessageDigest.getInstance("MD5"); digest.update(password.getBytes()); byte messageDigest[] = digest.digest(); StringBuffer MD5Hash = new StringBuffer(); for (int i = 0; i < messageDigest.length; i++) { String h = Integer.toHexString(0xFF & messageDigest[i]); while (h.length() < 2) h = "0" + h; MD5Hash.append(h); } mTextView.setText("Hasil MD5 : \n" + MD5Hash); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } } }
kalian juga bisa download source code nya download disini
jangan lupa tinggalkan komentar kalian disini
Tidak ada komentar:
Posting Komentar