Esempio n. 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)
	}
}
Esempio n. 2
0
// Test create token from command line.
func JwtCreate(data string, option *options.SysOption) {
	if key, err := option.GetPrivKey(); err == nil {
		parts := strings.Split(data, ".")
		if len(parts) == 2 {
			data := []string{
				base64.StdEncoding.EncodeToString([]byte(parts[0])),
				base64.StdEncoding.EncodeToString([]byte(parts[1]))}

			sig, err := jwt.SigningMethodRS256.Sign(strings.Join(data, "."), key)
			if err == nil {
				log.Printf(
					"\nSignature: %s\n\nToken: %s\n",
					sig,
					strings.Join(append(data, sig), "."))
			} else {
				log.Fatal(err)
			}
		} else {
			log.Println("Write two parts as tools data, format: 'header.payload'")
		}
	} else {
		log.Fatalf("Open key problem: %s\n", err)
	}
}