Пример #1
0
// Simple check keys.
func KeysSimpleCheck(data string, option *options.SysOption) {
	if privKey, err := option.GetPrivKey(); err == nil {
		if pubKey, err := option.GetPubKey(); err == nil {
			rand := helpers.NewSystemRandom()
			mainPart := fmt.Sprint(
				"%s.%s",
				base64.StdEncoding.EncodeToString([]byte(rand.CreatePassword(64))),
				base64.StdEncoding.EncodeToString([]byte(rand.CreatePassword(96))))

			sig, err := jwt.SigningMethodRS256.Sign(mainPart, privKey)
			if err == nil {
				err := jwt.SigningMethodRS256.Verify(mainPart, sig, pubKey)
				if err == nil {
					log.Printf("Keys from '%s' is correct\n", option.KeyDir)
				}
			} else {
				log.Fatalf("Can't ctrate signature: %s\n", err)
			}
		} else {
			log.Fatalf("Can't open public key! Error: %s\n", err)
		}
	} else {
		log.Fatalf("Can't open private key! Error: %s\n", err)
	}
}
Пример #2
0
// Test check token from command line.
func JwtCheck(data string, option *options.SysOption) {
	if key, err := option.GetPubKey(); err == nil {
		parts := strings.Split(data, ".")
		if len(parts) == 3 {
			if sigDta, err := base64.StdEncoding.DecodeString(parts[2]); err == nil {
				sig := string(sigDta)
				err := jwt.SigningMethodRS256.Verify(strings.Join(parts[0:2], "."), sig, key)
				if err == nil {
					log.Printf("\nCheck passed!\nSignature: %s\n", sig)
				} else {
					log.Fatal(err)
				}
			} else {
				log.Fatalf("Base64 decode problem: %s with: '%s'\n", err, parts[2])
			}
		} else {
			log.Println("Write a full token as tools data (3 parts)")
		}
	} else {
		log.Fatalf("Open key problem: %s\n", err)
	}
}