/**
 * @class
 * crypto
 */

import CryptoJS from "crypto-js";
import md5 from "md5";

class crypto{
	static MD5(string){
		console.log(string);
		return md5(string);
	}
	static enkey(string){
		let key = this.MD5(string).substr(8,16);
		let iv = this.MD5(key).substr(8,16);
		let keyIv = {
			"key": key,
			"iv": iv
		}
		return keyIv;
	}
	static encode(msg, keyIv){
		let message = '';
		if(typeof(msg) == 'object'){
			message = JSON.stringify(msg);
		}else{
			message = msg;
		}
		console.log(message);
    	let key = CryptoJS.enc.Utf8.parse(keyIv.key);
    	let iv = CryptoJS.enc.Utf8.parse(keyIv.iv);
    	let code = CryptoJS.AES.encrypt(message, key, { iv: iv},{ mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.nopadding });
		return code.toString();
	}
	static decode(code, keyIv){
		let key = CryptoJS.enc.Utf8.parse(keyIv.key);
    	let iv = CryptoJS.enc.Utf8.parse(keyIv.iv);;
		let decryptedData = CryptoJS.AES.decrypt(code, key, { iv: iv},{ mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.nopadding });
		return decryptedData.toString(CryptoJS.enc.Utf8);
	}
}

export default crypto;