Example #1
0
func Create(customSettings settings.Settings, data *DataToBeTokenized) (string, error) {
	err := customSettings.Check()

	if err != nil {
		return "", err
	}

	aesCipherBlock, _ := aes.NewCipher([]byte(customSettings.CipherKey))
	aesEncrypter := cipher.NewCFBEncrypter(aesCipherBlock, []byte("abcdefghabcdefgh"))

	serializedData := serialize(data)
	encryptedData := make([]byte, len(serializedData.Bytes()))

	aesEncrypter.XORKeyStream(encryptedData, serializedData.Bytes())
	return base64.StdEncoding.EncodeToString(encryptedData), nil
}
		}

		data := createToken.DataToBeTokenized{
			UserId: "user-123",
		}
		token, _ := createToken.Create(settings, &data)

		Expect(token).ToNot(BeNil())
	})

	It("Should not allow token creation if cipher key is different than 16, 24, or 32 bytes", func() {
		data := createToken.DataToBeTokenized{
			UserId: "User-12345",
		}

		settings := settings.Settings{CipherKey: "12345678"} //length = 8
		token, err := createToken.Create(settings, &data)
		Expect(token).To(Equal(""))
		Expect(err).ToNot(BeNil())

		settings.CipherKey = "12345678123456781234" //length = 20
		token, err = createToken.Create(settings, &data)
		Expect(token).To(Equal(""))
		Expect(err).ToNot(BeNil())

		settings.CipherKey = "1234567812345678123412345678123456781234" //length = 40
		token, err = createToken.Create(settings, &data)
		Expect(token).To(Equal(""))
		Expect(err).ToNot(BeNil())
	})