Find the key hash for a signed app

前端 未结 7 1488
隐瞒了意图╮
隐瞒了意图╮ 2020-11-29 17:31

I have signed my app and exported it to a folder on my desktop called app in this folder is my app itself and the keystore. How do i find the key hash that i can copy into t

相关标签:
7条回答
  • 2020-11-29 17:51

    The solutions mentioned above didn't work for me for some reason but i was able to successfully generate keyhash. I am writing the 10 easiest step to get keyhash of your signed apk [apk signed with keystore]:

    1. Copy the below code into your activity [start Activity].This code should be contained in your activity so that you can extract the proper keyhash when your signed apk's Activity starts.

      private void getHashKey() {
      try {
          PackageInfo info = getPackageManager().getPackageInfo(
                  getPackageName(), PackageManager.GET_SIGNATURES);
          for (Signature signature : info.signatures) {
              MessageDigest md = MessageDigest.getInstance("SHA");
              md.update(signature.toByteArray());
              Log.e("MY_KEY_HASH:",
                      Base64.encodeToString(md.digest(), Base64.DEFAULT));
          }
      } catch (NameNotFoundException e) {
      } catch (NoSuchAlgorithmException e) {
      } }
      
    2. Your AndroidManifest.xml file should have the attribute android:allowBackup="true" in its application tag.

    3. Export your signed apk and Install the app in your mobile phone and then connect your phone in debugging mode with usb debugging on.

    4. Then go to sdk\platform-tools

    5. Open command prompt and type adb devices to see whether your device is connected or not. if device not listed then try to fix that issue before continuing to next step.

    6. Then type adb logcat >"log.txt". Your cmd screen will like hanged. Dont Panic. Its Perfectly normal as the whole logcat is being written to log.txt.

    7. Run your app and hopefully when you think that the getHashKey() function is executed then press ctrl+c on command prompt to end log file writing.

    8. Now the command prompt will become responsive again. Now go into your sdk\platform-tools dir and you will see that a log.txt file has been created which contains logs.

    9. Now open it in a texteditor and search for MY_KEY_HASH:"-----------Your keyhash-------"

    10. Copy this onto your FB account or whereever you need it and then make another build in which the android:allowBackup="false" and getHashKey() function is removed.

    Hope this helped everyone :)

    0 讨论(0)
  • 2020-11-29 17:51

    if you use your alies use this command to find keystore alies

    keytool -list -v -keystore keystore.jks | findstr "Alias Creation"

    0 讨论(0)
  • 2020-11-29 17:54

    I couldnt be bothered with all this I see what the facebook sdk is actually sending by exporting my signed App with temp code in place to display facebook error.toString() in Authorize which gives the hash key its looking for I then put this in my facebook App and bingo !

    0 讨论(0)
  • 2020-11-29 18:09
    1. You should know where is your keystore file. For me is C:\Users\Selvin\Desktop\selvin.kp
    2. You should know your alias in keystore. For me is selvin
    3. You should know path to keytool. C:\Program Files\Java\jdk1.6.0_22\bin\keytool.exe
    4. You should know path to openssl. C:\OpenSSL-Win32\bin\openssl.exe
    5. You should know password to keystore. For me is ***** hehe

    Then, you should call:

    C:\Program Files\Java\jdk1.6.0_22\bin\keytool.exe" -exportcert -alias selvin -keystore c:\users\selvin\desktop\selvin.kp | C:\OpenSSL-Win32\bin\openssl sha1 -binary | C:\OpenSSL-Win32\bin\openssl base64

    Replace my path and alias with proper ones.

    Then you should see:

    Enter keystore password:

    Enter your password and you should get something like this: NfhDlIH7XWJzUZRjL+pZySrMX1Q=

    EDITED: NfgDlIG7XWJzUZRUL+bZySrMX1Q= <- is a bad hash. Or you got so lucky that your key made the same collision as

    error:keytool error: java.lang.Exception: Alias does not exist

    If hash not working:

    First, call

    C:\Program Files\Java\jdk1.6.0_22\bin\keytool.exe" -exportcert -alias selvin -keystore c:\users\selvin\desktop\selvin.kp

    Type password and read the error

    If you don't remember your alias keytool error: java.lang.Exception: Alias <selvinn> does not exist I used selvinn to show error.

    For a list of all your entries/aliases:

    C:\Program Files\Java\jdk1.6.0_22\bin\keytool.exe -list -keystore c:\users\selvin\desktop\selvin.kp

    second edit

    enter image description here

    0 讨论(0)
  • 2020-11-29 18:11

    For those still struggling I have found that these steps when followed correctly will certainly work but they can be quite challenging to get right first time, and actually I have found that sometimes the base64 conversion of the fingerprint when dealing with some alias' do not work (the hash gets truncated for some reason). I've written various batch files that pull most of these instructions already mentioned together so I don't rule out a problem there.

    However, essentially most people fall down at the openssl stage (either can't find it, don't know how to use it, or Windows piping doesn't correctly chain the output from the SHA1 export to the base64 conversion input).

    To get around this you can use an alternative method which is probably easier to follow and understand. Essentially what the facebook API wants is the base64 representation (encoding) of the SHA1 hash used to fingerprint your APK. To do this you can just list the keystore:

    "C:\Program Files\Java\JRE6\Bin\keytool.exe" -list -v -keystore "Path-to-your-keystore" -storepass "KeystorePassword" > somefile.txt
    

    Obviously you need to change the path to the keytool executable according to your own setup, and replace "Path-to-your-keystore" and "KeystorePassword" with your keystore path and password! The result should be the creation of a file "sometext.txt" in the current folder which you can then open in any text editor. The text file will list all of the keystore alias' and their respective MD5 and SHA1 hashes as hex strings.

    Now just find the alias used to sign your APK, copy the SHA1 hash, and use any online hex to base64 converter to convert it to the base64 encoding format that facebook requires. You can find an online converter by googling "online hex to base64 converter". I've been using this one as you can just copy and paste the string right from the text file in to the box provided and it will just remove the colons that separate each hex byte.

    One final point (somewhat obvious but..) only copy and paste the hex string and NOT the SHA1: prefix!

    Hope this helps someone; it certainly works for me!

    0 讨论(0)
  • 2020-11-29 18:13

    First i would like to thank Selvin!

    This answer is almost identical to Selvin's answer but it still took me 3 hours to get it to work :P so a little more tutorial for the real newbs

    How to get a hashKey from a keystore

    • first install OpenSSL from google code and put it in your C:\ folder download link
    • find keytool program location (default in java folder)
    • find keystore location (there is a debug default value)

    location keytool C:\Program Files (x86)\Java\jdk1.xxx)\bin\keytool location openssl C:\OpenSSL-Win32\bin\OpenSSL location (debug)keystore C:\Users[usernamepc].android\debug.keystore

    open cmd in windows(start->run->cmd) and navigate to the keytool location or copy paste the following string, note that you can’t use ctrl+v but right mouseclick

    cd c:\program files (x86)\java\jdk1.7.0_01\bin
    

    when in the right directory paste this string in the cmd line:

    keytool.exe -exportcert -alias androiddebugkey -keystore c:\users\charx\.android\debug.keystore | C:\OpenSSL-Win32\bin\openssl sha1 -binary | C:\OpenSSL-Win32\bin\openssl base64
    

    "C:\OpenSSL-Win32\bin\openssl" is the path of opensssl.exe replace it with your openssl.exe's path

    make sure that you change the [usernamepc] of yourto the name of your pc as you can see in my case it’s charx. Also the directory for the java jdk xxxx depends on your version.

    cmd should show hashkey

    hash key for my debug file is

    h1GdQbgB8b/liCG+acmZWkgIRHA=
    
    0 讨论(0)
提交回复
热议问题