/**
 * @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 = JSON.stringify(msg);
    	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 JSON.parse(decryptedData.toString(CryptoJS.enc.Utf8));
	}
}

export default crypto;