generated from cubetiq/ts-project
124 lines
7.4 KiB
TypeScript
124 lines
7.4 KiB
TypeScript
import { Crypto, DefaultCryptoProvider, E2ECryptoProvider } from "../src"
|
|
|
|
// DefaultCryptoProvider
|
|
const key = "67rKmuc6DiDukE0jsUP421Eizo4CreaL6Q7Pg/NmH/s="
|
|
const iv = "FEFM9AY2m5jDq6GZ+CfLIA=="
|
|
const keyJsonFileValuesDefault = "./dummy/aes-256.json"
|
|
const keyJsonFileKeysDefault = "./dummy/aes-256.keys.json"
|
|
|
|
// E2ECryptoProvider
|
|
const PRIVATE_KEY_VALUE =
|
|
"-----BEGIN RSA PRIVATE KEY----- MIIJKQIBAAKCAgEA0PfjoO54+QZui0EfssxGvHOm8bhDzQIIDcH/jUvN25E28MYF VOy1CgDk2n25eakhaaK8ogmP9PfczGpLalCGfVACCXNQFc60k6zjQY8lNY6DY2NJ 4qGR5OZsA2lhXgYtptqHCvGnqqIBpfYkddKzZOjQGZvUQ3XVZvEpkDuJGvPFW/+B 4NjsgSK0bJzygnJnbJYUHezBVRQynJRIxJmCZfuhqJM2w0FLB1/8TZVn9w3OC3DX avU7DfEGOlJb9evRrvdVGag14Pcy6SXoZ1jFscRJuXyWy1mLooTx6osj7V6VYhnK ZJrlRTAUxnGZLuBdqYztU7SNhZfmuCSIS+nduqrdM9P3NPqgOqmIMTps0fcQg0DU 5CFdWIy/FecErg1YaAhbEOqQO4QQlTmMDFFJ63Y9rE3s9W5k30ZP8Ydxa8UT3su3 6ePzUGzWXUu2M8ffc8nnfiAvh1XgJiBqDJkmVEG3GzmDL0BfDLqMnhBJ2UaQPXK9 w52masDgvHvyMEZoBaFBHtVICl4Jek6O9KSz1uWcR/+C72fBEU4iA/h4rdH4PVMT P99YhN+Zd4w9UM2tITafCM3RdNbKZ3f+oXfFCjqNKiJI/VSLkErOKQnw/AiGT8dp mmFGl+DazUsYNEP3ScFUAI+ZqsUyh5q9SoCnYmcpuMbXLRLVYQJQASkYwBkCAwEA AQKCAgEAljZ7N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----END PUBLIC KEY-----"
|
|
const privatePublicJsonFileValuesDefault = "./dummy/private-public.json"
|
|
const privatePublicJsonFileKeysDefault = "./dummy/private-public.keys.json"
|
|
const privateKeyPath = "./dummy/private-key.pem"
|
|
const publicKeyPath = "./dummy/public-key.pem"
|
|
|
|
// Text
|
|
const TEXT = `Hello World and Then...!`
|
|
|
|
test("test: Default Encryption From Values", () => {
|
|
const provider = Crypto.newInstance(
|
|
new DefaultCryptoProvider({
|
|
key: key,
|
|
iv: iv,
|
|
})
|
|
)
|
|
|
|
const encrypted = provider.encrypt(TEXT)
|
|
const decrypted = provider.decrypt(encrypted)
|
|
expect(decrypted).toBe(TEXT)
|
|
})
|
|
|
|
test("test: Default Encryption with Hello World", () => {
|
|
const data = "Hello World"
|
|
const provider = Crypto.newInstance(
|
|
new DefaultCryptoProvider({
|
|
key: key,
|
|
iv: iv,
|
|
})
|
|
)
|
|
|
|
const encrypted = provider.encrypt(data)
|
|
const decrypted = provider.decrypt(encrypted)
|
|
const decrypted2 = provider.decrypt("6Rkfueq+fT3vPgWTST1lXg==")
|
|
expect(decrypted).toBe(data)
|
|
expect(decrypted2).toBe(data)
|
|
})
|
|
|
|
test("test: Default Encryption From JsonFile Values", () => {
|
|
const provider = Crypto.newInstance(
|
|
new DefaultCryptoProvider({
|
|
keyiVPath: keyJsonFileValuesDefault,
|
|
})
|
|
)
|
|
|
|
const encrypted = provider.encrypt(TEXT)
|
|
const decrypted = provider.decrypt(encrypted)
|
|
expect(decrypted).toBe(TEXT)
|
|
})
|
|
|
|
test("test: Default Encryption From JsonFile Keys Path", () => {
|
|
const provider = Crypto.newInstance(
|
|
new DefaultCryptoProvider({
|
|
jsonPath: keyJsonFileKeysDefault,
|
|
})
|
|
)
|
|
|
|
const encrypted = provider.encrypt(TEXT)
|
|
const decrypted = provider.decrypt(encrypted)
|
|
expect(decrypted).toBe(TEXT)
|
|
})
|
|
|
|
test("test: E2E Encryption From Values", () => {
|
|
const provider = Crypto.newInstance(
|
|
new E2ECryptoProvider({
|
|
publicKey: PUBLIC_KEY_VALUE,
|
|
privateKey: PRIVATE_KEY_VALUE,
|
|
})
|
|
)
|
|
|
|
const encrypted = provider.encrypt(TEXT)
|
|
const decrypted = provider.decrypt(encrypted)
|
|
expect(decrypted).toBe(TEXT)
|
|
})
|
|
|
|
test("test: E2E Encryption From Files Values", () => {
|
|
const provider = Crypto.newInstance(
|
|
new E2ECryptoProvider({
|
|
privateKeyPath: privateKeyPath,
|
|
publicKeyPath: publicKeyPath,
|
|
})
|
|
)
|
|
|
|
const encrypted = provider.encrypt(TEXT)
|
|
const decrypted = provider.decrypt(encrypted)
|
|
expect(decrypted).toBe(TEXT)
|
|
})
|
|
|
|
test("test: E2E Encryption From JsonFile Values", () => {
|
|
const provider = Crypto.newInstance(
|
|
new E2ECryptoProvider({
|
|
jsonPath: privatePublicJsonFileValuesDefault,
|
|
})
|
|
)
|
|
|
|
const encrypted = provider.encrypt(TEXT)
|
|
const decrypted = provider.decrypt(encrypted)
|
|
expect(decrypted).toBe(TEXT)
|
|
})
|
|
|
|
test("test: E2E Encryption From JsonFile Keys Path", () => {
|
|
const provider = Crypto.newInstance(
|
|
new E2ECryptoProvider({
|
|
jsonPath: privatePublicJsonFileKeysDefault,
|
|
})
|
|
)
|
|
|
|
const encrypted = provider.encrypt(TEXT)
|
|
const decrypted = provider.decrypt(encrypted)
|
|
expect(decrypted).toBe(TEXT)
|
|
})
|