当前位置 : 主页 > 手机开发 > android >

Android – SMS Retriever API – 计算应用程序的哈希字符串问题

来源:互联网 收集:自由互联 发布时间:2021-06-11
我是 Android的新手,我正在尝试实现SMS Retriever API,以便在我的应用中使用OTP. 我正在遵循本指南: https://developers.google.com/identity/sms-retriever/verify#computing_your_apps_hash_string 不幸的是,我陷入了
我是 Android的新手,我正在尝试实现SMS Retriever API,以便在我的应用中使用OTP.

我正在遵循本指南:
https://developers.google.com/identity/sms-retriever/verify#computing_your_apps_hash_string

不幸的是,我陷入了“计算你的应用程序的哈希字符串”的部分

我在这里引用了指南部分,并在每个部分下面 – 我的问题:

>将您的应用程序的公钥证书作为小写十六进制字符串.例如,要从密钥库中获取十六进制字符串,请键入以下命令

keytool -alias MyAndroidKey -exportcert -keystore MyProduction.keystore | xxd -p | tr -d "[:space:]"

我在哪里可以找到我的“公钥证书”,我应该在哪里运行此命令?

>计算组合字符串的SHA-256总和.

什么是SHA-256,计算它意味着什么?

> Base64-编码SHA-256总和的二进制值.您可能需要首先从其输出格式解码SHA-256和.

无法理解,我应该在这做什么?

import android.content.Context;
import android.content.ContextWrapper;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.os.Build;
import android.util.Base64;
import android.util.Log;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;


/*
  This is a helper class to generate your message hash to be included in your SMS message.

  Without the correct hash, your app won't recieve the message callback. This only needs to be
  generated once per app and stored. Then you can remove this helper class from your code.
*/

public class AppSignatureHelper extends ContextWrapper {
    public static final String TAG = AppSignatureHelper.class.getSimpleName();
    private static final String HASH_TYPE = "SHA-256";
    public static final int NUM_HASHED_BYTES = 9;
    public static final int NUM_BASE64_CHAR = 11;

    public AppSignatureHelper(Context context) {
        super(context);
        getAppSignatures();
    }

    /**
     * Get all the app signatures for the current package
     * @return
     */
    public ArrayList<String> getAppSignatures() {
        ArrayList<String> appCodes = new ArrayList<>();

        try {
            // Get all package signatures for the current package
            String packageName = getPackageName();
            PackageManager packageManager = getPackageManager();
            Signature[] signatures = packageManager.getPackageInfo(packageName,
                    PackageManager.GET_SIGNATURES).signatures;

            // For each signature create a compatible hash
            for (Signature signature : signatures) {
                String hash = hash(packageName, signature.toCharsString());
                if (hash != null) {
                    appCodes.add(String.format("%s", hash));
                }

                Log.d(TAG, "Hash " + hash);

            }
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(TAG, "Unable to find package to obtain hash.", e);
        }
        return appCodes;
    }

    private static String hash(String packageName, String signature) {
        String appInfo = packageName + " " + signature;
        try {
            MessageDigest messageDigest = MessageDigest.getInstance(HASH_TYPE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                messageDigest.update(appInfo.getBytes(StandardCharsets.UTF_8));
            }
            byte[] hashSignature = messageDigest.digest();

            // truncated into NUM_HASHED_BYTES
            hashSignature = Arrays.copyOfRange(hashSignature, 0, NUM_HASHED_BYTES);
            // encode into Base64
            String base64Hash = Base64.encodeToString(hashSignature, Base64.NO_PADDING | Base64.NO_WRAP);
            base64Hash = base64Hash.substring(0, NUM_BASE64_CHAR);

            Log.d(TAG, String.format("pkg: %s -- hash: %s", packageName, base64Hash));
            return base64Hash;
        } catch (NoSuchAlgorithmException e) {
            Log.e(TAG, "hash:NoSuchAlgorithm", e);
        }
        return null;
    }
}

在项目中添加上面的类,然后从LoginActivity中调用它,如下所示

AppSignatureHelper appSignatureHelper = new AppSignatureHelper(LoginActivity.this);然后你会得到11位哈希

网友评论