title: Sign in Java and verify in Go Lang
body: <p>I have a logic to sign data in Java as below:</p>
  
  <pre><code>public String encryptWithRSA(String data) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, IOException, InvalidKeySpecException, SignatureException {
          Signature privateSignature = Signature.getInstance("SHA256withRSA");
          privateSignature.initSign(getPrivateKey());
          privateSignature.update(data.getBytes("UTF-8"));
          byte[] signature = privateSignature.sign();
          return Base64.getEncoder().encodeToString(signature);
      }
  </code></pre>
  
  <p>I need to verify the signature in Go Lang. I have the code as below:</p>
  
  <pre><code>func validData(tcktsignature string, ticketInfo string) bool {
  
      message := []byte(ticketInfo)
      hashed := sha256.Sum256(message)
      log.Printf("Hash value = %x",hashed)
  
      signature, _ := base64.StdEncoding.DecodeString(tcktsignature)
      err := rsa.VerifyPKCS1v15(getPublicKey(), crypto.SHA256, hashed[:], signature)
      if err != nil {
          log.Printf("Error from verification: %s\n", err)
          return false
      }
      return true
  }
  
  func getPublicKey() (PublicKey *rsa.PublicKey) {
  
      pubPEM := getFileContent(pubKeyPath)
      key, _ := base64.StdEncoding.DecodeString(pubPEM)
      re, err := x509.ParsePKIXPublicKey(key)
      if err != nil {
          log.Println(err)
      }
      pub := re.(*rsa.PublicKey)
      return pub
  }
  </code></pre>
  
  <p>But Go Lang gives me the error : <strong>Error from verification: crypto/rsa: verification error</strong></p>
  
  <p>I compared the Has value generated in both Java and Go Lang are same. But I am not sure on why it is failing. Can someone help me out?</p>
  
  <p>The private key in Java is generated in the following fashion:</p>
  
  <pre><code>KeyPair generateRSAKeys() {
          KeyPair keyPair = null;
          try {
              File privateKeyFile = new File(PRIVATE_KEY_FILENAME);
              File publicKeyFile = new File(PUBLIC_KEY_FILENAME);
  
              if (!privateKeyFile.exists() || !publicKeyFile.exists()) {
                  KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
                  kpg.initialize(2048);
                  keyPair = kpg.generateKeyPair();
                  writeToFile(PRIVATE_KEY_FILENAME, keyPair.getPrivate().getEncoded());
                  writeToFile(PUBLIC_KEY_FILENAME, keyPair.getPublic().getEncoded());
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
          return keyPair;
      }
  </code></pre>