C_U*_*C_U 4 shell java openssl binary
假设我将字符串转换为字节数组。
\n\nbyte[] byte sUserID.getBytes(\xe2\x80\x9cUTF-8\xe2\x80\x9d); //Convert User ID String to byte array \nRun Code Online (Sandbox Code Playgroud)\n\n现在我需要在 Shell 上编写一个脚本,该脚本与我的 Java 代码具有完全相同的功能。在某个阶段,我必须对我的字节数组进行哈希处理(MessageDigest.getInstance(\xe2\x80\x9cSHA-256\xe2\x80\x9d)在 Java 和openssl dgst -sha256 \xe2\x80\x93binaryShell 中使用),但是因为 Java 代码中的摘要是从字节数组生成的,所以它们不会与我在 Shell 中得到的\xe2\x80\x99t 匹配结果(在 Shell 中我只是哈希目前是字符串,因此输入格式不匹配)。
因为我openssl在shell中的输入应该类似于Java输入我想知道是否有办法getBytes()在Shell中\xe2\x80\x9csimulate\xe2\x80\x9d方法?我在 Shell 方面没有太多经验,所以我不知道在这种情况下最好的方法是什么。有任何想法吗?干杯!
openssl\ 的 stdin 是字节流。
的内容$user是非 0 字节序列(可能会也可能不会形成 UTF-8 或其他字符集/编码中的有效字符)。
printf %s "$user"\ 的标准输出是字节流。
printf %s "$user" | openssl dgst -sha256 \xe2\x80\x93binary\nRun Code Online (Sandbox Code Playgroud)\n\n将连接printf\ 的标准输出与openssl\ 的标准输入。openssl\ 的标准输出是另一个字节流。
$user现在,如果您从终端从用户输入,用户将通过按下键盘上的按键来输入。终端将发送以其配置的字符集编码的相应字符(如按键标签上所写)。通常,该字符集将基于当前区域设置中的字符集。你可以找到那是什么locale charmap。
例如,对于诸如 之类的语言环境fr_FR.iso885915@euro,并且xterm在该语言环境中启动,locale charmap将返回ISO-8859-15。如果用户st\xc3\xa9phane作为用户名输入,则\xc3\xa9可能会被编码为0xe9字节,因为这就是它在字符集中的定义方式ISO-8859-15。
如果您希望\xc3\xa9在传递给 之前将其编码为 UTF-8 openssl,则可以在此处iconv将该字节转换0xe9为 UTF-8 中相应的编码(两个字节:)0xc3 0xa9:
IFS= read -r user # read username from stdin as a sequence of bytes\n # assumed to be encoded from characters as per the\n # locale\'s encoding\nprintf %s "$user" |\n iconv -t utf-8 | # convert from locale encoding to UTF-8\n openssl dgst -sha256 \xe2\x80\x93binary \nRun Code Online (Sandbox Code Playgroud)\n