hash md5结果没有变化

时间:2015-09-14 13:15:09

标签: java android md5

我的Android应用程序是校验和哈希md5。一切看起来都很有效,但每次我点击"生成md5"按钮,哈希永远不会改变。生成的哈希似乎是一个空的"字符(d41d8cd98f00b204e9800998ecf8427e)。这是我的代码。谢谢你的帮助。

package com.example.kalkulator;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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 static final String TAG = "MD5";
    //private TextView textView_hasil;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button1 = (Button)findViewById(R.id.button1);
        Button button2 = (Button)findViewById(R.id.button2);
        final EditText editText1 = (EditText)findViewById(R.id.editText1);
        final TextView textView3 = (TextView)findViewById(R.id.textView3);

        final String EditText1 = editText1.getText().toString();

        button1.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                hitungMD5(EditText1);
            }

            private void hitungMD5(String editText1) {
                // TODO Auto-generated method stub
                try
                {
                    MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
                    digest.update(editText1.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);
                    }

                    textView3.setText("Hash Anda: " + MD5Hash);
                }
                catch(NoSuchAlgorithmException e)
                {
                    e.printStackTrace();
                }
            }


        });

        button2.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                editText1.setText("");
                textView3.setText("");
            }
        });

    }

2 个答案:

答案 0 :(得分:1)

每次单击按钮时,您可能希望更新传递给hitungMD5的字符串。而不是

hitungMD5(EditText1);

使用

hitungMD5(editText1.getText().toString());

当您按下按钮时,这将传递到hitungMD5 editText1的实际内容

答案 1 :(得分:0)

您正在使用EditText1中的变量onClick(View v),但您永远不会更新它。您需要将editText1.getText()移至onClick(View v)

button1.setOnClickListener(new OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        String text = editText1.getText().toString();
        hitungMD5(text);
    }