简体   繁体   English

在MainActivity.java文件中向哈希密码添加代码的位置

[英]Where to add code to hash password in my MainActivity.java file

I have to create an Activity that fetches Username and Password from the User. 我必须创建一个从用户获取用户名和密码的Activity Then it applies an MD5 hash to the Password and finally queries the local database to return "true" or "false". 然后它将MD5哈希值应用于密码,最后查询本地数据库以返回“true”或“false”。

I got the MD5 code snippet from a blog . 我从博客中获得了MD5代码段。 The code snippet is showing the following errors on eclipse: 代码片段在eclipse上显示以下错误:

Marked line 1: Multiple markers at this line 标记的第1行:此行的多个标记

  • Syntax error on token(s), misplaced construct(s) 令牌上的语法错误,错放的构造(s)
  • Syntax error, insert "enum Identifier" to complete EnumHeader 语法错误,插入“enum Identifier”以完成EnumHeader
  • Syntax error on token "String", @ expected 令牌“String”上的语法错误,@ expected
  • Syntax error on token "String", @ expected 令牌“String”上的语法错误,@ expected

Marked line 2: Syntax error on token(s), misplaced construct(s) 标记第2行:令牌上的语法错误,错放的构造(s)

Marked line 3: Syntax error on token(s), misplaced construct(s) 标记第3行:令牌上的语法错误,错放的构造(s)

Marked line 4: Multiple markers at this line 标记的第4行:此行的多个标记

  • Syntax error on token "catch", Identifier expected 令牌“catch”上的语法错误,预期的标识符
  • Syntax error on token "}", { expected after this token 令牌“}”上的语法错误,{此标记后的预期

I am extremely new to java and also the Android environment. 我是java和Android环境的新手。 Please tell me what is wrong with the code and where should I place the code for the md5 hashing function for my activity to work. 请告诉我代码有什么问题,我应该在哪里放置md5哈希函数的代码,以便我的活动正常工作。 Also please guide me towards quering a local SQLite Database and returning required values from there. 另请指导我查询本地SQLite数据库并从那里返回所需的值。

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

private String md5(String in) {  //line 1
    MessageDigest digest;        //line 2
    try {                        //line 3
        digest = MessageDisgest.getInstance("MD5");
        digest.reset();
        digest.update(in.getBytes());
        byte[] a = digest.digest();
        int len = a.length;
        StringBuilder sb = new StringBuilder(len << 1);
        for(int i=0;i<len;i++) {
            sb.append(Character.forDigit((a[i] & 0xf0) >> 4, 16));
            sb.append(Character.forDigit(a[i] & 0x0f, 16));
        }
        return sb.toString();
    } catch (NoSuchAlgorithmException e) { e.printStackTrace();} //line 4
    return null;

}

public class MainActivity extends Activity {

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



    Button btn = (Button) findViewById(R.id.button1);
    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            EditText text = (EditText)findViewById(R.id.editText1);
            EditText text1 = (EditText)findViewById(R.id.editText2);
            String userid = text.getText().toString();
            String pass = text1.getText().toString();
        Toast.makeText(MainActivity.this,"Entered"+userid+"and password entered is"+pass,Toast.LENGTH_SHORT).show();
        }

    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

In Java, all methods must exist inside a class. 在Java中,所有方法都必须存在类中。

public class MainActivity extends Activity {
    private String md5(String in) {
        // etc
    }

    // Rest of class
}

I am extremely new to java and also the android environment. 我是java和android环境的新手。

You will find many benefits from reading a tutorial or introduction, like Learning the Java Language from the language's authors. 您可以从阅读教程或介绍中找到许多好处,例如从语言作者那里学习Java语言

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM