How can I retrieve a password such that it can sec

2019-08-13 03:32发布

问题:

How can I retrieve a password from user input in Lua in a way that it can securely be deleted from memory after the program is done using the password?

This is a followup to: lua aes encryption

How can I convert a password to hexadecimals in lua?

A simple example would be:

"pass" becomes {0x70,0x61,0x73,0x73}

回答1:

What do you mean by "hexadecimals"? Do you want to convert pass to a string containing #pass*2 hexadecimal characters? Then you want this:

function toHex(s)
    return (string.gsub(s, ".", function (c)
        return string.format("%02X", string.byte(c))
      end))
end
print(toHex('password')) --> 70617373776F7264

Or do you want a table of numbers, where each number is one character code (byte)? Then you want this:

function toBytes(s)
    return {string.byte(s, 1, #s)}
end
print(table.concat(toBytes('password'), ',')) --> 112,97,115,115,119,111,114,100