title: How to generate KeyPair in android KeyStore, protected by custom password
body: <p>I have app which uses Fingerprint and 4 digit pin (which can be set by user in application settings, this is not system pin/pattern/password) authorization.</p>
  
  <p>With Fingerprint everything is fine, I have problem with pin.</p>
  
  <p>What I'm trying to do is:</p>
  
  <ol>
  <li>Let user set pin from application settings</li>
  <li>Generate Keypair in keystore for some alias protected by entered pin (so that private key can not be accessed without this pin)</li>
  <li>Send public key to server for future signature verification</li>
  <li>Get user token from server and store it in local storage</li>
  </ol>
  
  <p>Then during Authorization:</p>
  
  <ol start="5">
  <li>Request user to enter the pin</li>
  <li>Retrieve private key by entered pin</li>
  <li>Retrieve token from local storage</li>
  <li>Sign token with private key from step 6</li>
  <li>Send it to server with signed data</li>
  <li>Verify signature on server and if it is valid, open session for user.</li>
  </ol>
  
  <p>My question is: <strong>How can I generate KeyPair protected by pin entered by user</strong></p>
  
  <p>P.S. For <a href="https://github.com/googlesamples/android-AsymmetricFingerprintDialog" rel="noreferrer">fingerprint</a> this logic is handled by:</p>
  
  <pre><code>KeyGenParameterSpec.Builder(KEY_NAME, KeyProperties.PURPOSE_SIGN).setUserAuthenticationRequired(true)
  </code></pre>