Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Golang Support #7

Open
kushalbikriapp opened this issue Oct 3, 2020 · 2 comments
Open

Golang Support #7

kushalbikriapp opened this issue Oct 3, 2020 · 2 comments

Comments

@kushalbikriapp
Copy link

Can we see the golang implementation for this?

@kushalhalder
Copy link

func FetchPublicKey() (publicKey *TPublicKeyResponse, err error) {
	req := httplib.Get(beego.AppConfig.String("TRUECALLER_PUBLIC_KEY_URL"))
	req.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
	req.Debug(true)

	str, err := req.String()
	if err != nil {
		l.Printf("Error: %v", err)
		return nil, err
	}
	l.Println(str)
	pr := TPublicKeyResponse{}
	_ = json.Unmarshal([]byte(str), &pr)
	return &pr, nil
}

func VerifyPayload(s *SDKResponse) bool {
	publicKeyString, err := FetchPublicKey()
	if err != nil {
		l.Printf("Error: %v", err)
		return false
	}
	publicKeyInterface, err := x509.ParsePKIXPublicKey([]byte(publicKeyString.Key))
	publicKey, isRSAPublicKey := publicKeyInterface.(*rsa.PublicKey)
	if !isRSAPublicKey {
		l.Printf("isRSAPublicKey: %v", isRSAPublicKey)
		return false
	}
	hashed := sha256.Sum256([]byte(s.Payload))
	sDec, esDec := base64.StdEncoding.DecodeString(s.Signature)
	if esDec != nil {
		l.Printf("Signature Decode Error: %v", esDec)
		return false
	}
	e := rsa.VerifyPKCS1v15(publicKey, crypto.SHA512, hashed[:], sDec)
	if e != nil {
		l.Printf("Verify: %v", e)
		return false
	}

	return true
}

This is what I have, but it fails at
publicKey, isRSAPublicKey := publicKeyInterface.(*rsa.PublicKey)

@kushalhalder
Copy link

The error at publicKeyInterface, err := x509.ParsePKIXPublicKey([]byte(publicKeyString.Key)) is PublicKey Parse error: asn1: syntax error: sequence truncated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants