为什么字符串的utf8编码给出了不变的字符串

时间:2017-06-07 05:18:23

标签: node.js utf-8

我的代码如下所示:

let createCipher = (req, res) => {
    const text = req.body.text;
    let hash = crypto.createHash('md5').update(utf8.encode(text)).digest('hex');
    res.json({
        status: '200',
        data: hash,
        utf: utf8.encode(text)
    });
}

在这里,无论我提供什么 text 输入参数,都会在响应字段 utf 中返回。所以utf8进程中有什么缺失吗?

1 个答案:

答案 0 :(得分:1)

let createCipher = (req, res) => {
    const text = req.body.text;
    //text = "Hello World";

    let hash = crypto.createHash('md5');
    hash.update(utf8.encode(text));

    //or you try
    //let hash = crypto.createHash('md5').update(utf8.encode(text));

    let data = hash.digest('hex');
    //console.log(data + '  ' + text);
    //console.log("utf8 - byte data : " + utf8.encode(data)+ '  ' + utf8.encode(text));
    res.json({
        status: '200',
        data: hash,
        utf: utf8.encode(data) //data
    });
}

//Output 
//b10a8db164e0754105b7a99be72e3fe5  Hello World                               
//utf8 - byte data : b10a8db164e0754105b7a99be72e3fe5  Hello World