const { decryptFileAES,encryptFileAES,encryptDataRSA_AES,encryptDataAES: encryptData, decryptDataAES: decryptData,generateKeyPair, encryptDataRSA: encryptRSA, decryptDataRSA: decryptRSA, encryptFileRSA_AES, decryptFileRSA_AES, encryptDataAES, encryptDataRSA, decryptDataAES, decryptDataRSA_AES } = require('../encriptacao/encryptUtils')

const fs = require('fs')

const {v4:uuidv4} = require('uuid')

xit("teste encriptacao",async function() {
    const crypto = require('crypto')

    const key  = Buffer.alloc(32,2)
    const iv = Buffer.alloc(16,1)
    const pass = "1234"
    const passFixed = crypto.createHash('sha256').update(pass).digest()

    const cipher = crypto.createCipheriv('aes256',passFixed,iv)
    
    let keyEnc = cipher.update(Buffer.concat([iv,key]).toString('base64'),'utf8','hex')
    keyEnc += cipher.final('hex')

    console.log(keyEnc)

    const decipher = crypto.createDecipheriv('aes256',passFixed,iv)
    let keyClean = decipher.update(keyEnc,'hex','utf8')
    keyClean += decipher.final('utf8')

    console.log(Buffer.from(keyClean,'base64'))

    // console.log(keyClean)
})

xit("teste encryptData/decryptData",async function() {
    const key = "aHQsHVCVa5CS7TPH"
    const pass = "senhadamu"

    const keyEnc = await encryptData({data:key,password: pass})

    console.log('dataEnc:',keyEnc)

    const keyClean = await decryptData({dataEnc:keyEnc,password: pass})

    console.log('dataClean: ' + keyClean)
})

xit("teste desencriptar chave mu",async function() {

    const muKeyEnc = require("../encriptacao/microuniversoMasterKeyEnc")

    const pass = "senhadamu"

    const muKeyClean = await decryptData({dataEnc: muKeyEnc,password: pass})

    console.log(muKeyClean)
})

xit("gerar pares de chaves",async function() {
    const {publicKey,privateKey} = await generateKeyPair({privateKeyPassword: "foo"})

    console.log(publicKey)
    console.log(privateKey)
})

xit("criptografar usando rsa",async function() {
    const privateKeyPassword = "microuniverso"
    const {publicKey,privateKey} = await generateKeyPair({privateKeyPassword: privateKeyPassword})

    const data = "foo bar baz"

    const dataEnc = await encryptRSA({data,publicKey})

    console.log('dataEnc:',dataEnc)

    const dataDenc = await decryptRSA({dataEnc,privateKey,privateKeyPassword})

    console.log('dataDenc: ',dataDenc)

})

xit("encryptFileRSA_AES/decryptFileRSA_AES test",async function() {
    const filePathPlainText = "c:/temp/" + uuidv4()
    const filePathEncrypted = filePathPlainText + ".enc"
    const filePathDecrypted = filePathPlainText + ".denc"
    const privateKeyPassword = "microuniverso"

    fs.writeFileSync(filePathPlainText,"foo bar baz",{encoding: 'utf8'})  

    const {publicKey,privateKey} = await generateKeyPair({privateKeyPassword})

    await encryptFileRSA_AES({
        filePathPlainText: filePathPlainText,
        filePathEncrypted: filePathEncrypted,
        publicKey: publicKey
    })

    await decryptFileRSA_AES({
        filePathEncrypted: filePathEncrypted,
        filePathDecrypted: filePathDecrypted,
        privateKey: privateKey,
        privateKeyPassword: privateKeyPassword
    })

    const decryptedFileData = fs.readFileSync(filePathDecrypted,{encoding: 'utf8'})

    console.log('decrypted content', decryptedFileData)

    
})

xit("encryptDataRSA_AES test",async function() {
    const privateKeyPassword = "microuniverso"
    const {publicKey,privateKey} = await generateKeyPair({privateKeyPassword})

    const data = "foo bar baz"

    const dataEnc = await encryptDataRSA_AES({
        data: data,
        publicKey: publicKey
    })

    console.log(dataEnc)
})

xit("encryptDataAES/decryptDataAES test", async function() {
    const data = "foo bar baz"
    const password = "1234"

    const dataEnc = await encryptDataAES({
        data,password
    })

    console.log('dataEnc',dataEnc)

    const dataDenc = await decryptDataAES({
        dataEnc,password
    })

    console.log('dataDenc',dataDenc)
})

xit("encryptDataRSA_AES/decryptDataRSA_AES test", async function() {
    const data = "foo bar baz"
    const privateKeyPassword = "1234"
    const {publicKey,privateKey} = await generateKeyPair({privateKeyPassword})

    const dataEnc = await encryptDataRSA_AES({
        data,publicKey
    })

    console.log('dataEnc',dataEnc)

    const dataDenc = await decryptDataRSA_AES({
        data: dataEnc,privateKey,privateKeyPassword
    })

    console.log('dataDenc',dataDenc)
})

it("encryptDataAES/decryptDataAES",async function() {
    const rightPassword = "1234"
    const wrongPassword = "errada"
    const data = "foo bar baz"

    const dataEnc = await encryptDataAES({
        data,
        password: rightPassword
    })

    try {
        await decryptDataAES({
            dataEnc,
            password: wrongPassword
        })

        throw new Error("DEVERIA TER DISPARADO EXCECAO")
        
    } catch (error) {
        console.log('OK')
    }


})

xit("encryptFileAES/decryptFileAES test",async function() {
    const filePathPlainText = "c:/temp/" + uuidv4()
    const filePathEncrypted = filePathPlainText + ".enc"
    const filePathDecrypted = filePathPlainText + ".denc"
    const password = "microuniverso"

    fs.writeFileSync(filePathPlainText,"foo bar baz",{encoding: 'utf8'})  

    await encryptFileAES({
        filePathPlainText: filePathPlainText,
        filePathEncrypted: filePathEncrypted,
        password
    })

    console.log('encrypted')

    await decryptFileAES({
        filePathEncrypted,
        filePathDecrypted,
        password
    })

    const decryptedFileData = fs.readFileSync(filePathDecrypted,{encoding: 'utf8'})

    console.log('decrypted content', decryptedFileData)
})