const API = require("../../lib/API");
const { RSAUtils } = require('../../plugin/RSAUtils/RSAUtils')
const { BaseStdResponse } = require("../../BaseStdResponse");

class EncrypedPwd extends API {
    constructor() {
        super();

        this.noEncrypt()
        this.setPath('/Public/EncrypedPwd')
        this.setMethod('POST')
    }

    async onRequest(req, res) {
        let { modulus, password } = req.body

        if ([modulus, password].some(value => value === '' || value === null || value === undefined))
            return res.json({
                ...BaseStdResponse.MISSING_PARAMETER
            })

        let key = RSAUtils.getKeyPair('10001', "", modulus)
        const reversedPwd = password.split('').reverse().join('')
        const encryptedPwd = RSAUtils.encryptedString(key, reversedPwd)

        res.json({
            ...BaseStdResponse.OK,
            password: encryptedPwd
        })
    }
}

module.exports.EncrypedPwd = EncrypedPwd