Pgcrypto命令产生非标准输出

问题描述 投票:2回答:1

每次运行'encrypt'或'digest'命令时,我的输出都包含多个字符'\'实例。

参见示例“select encrypt('123456789012345','1234','aes');” - 预期产出:“\ x34591627f9c8eae417fc7cbbf458592c”

而我的输出如下:“4Y \ 026'\ 371 \ 310 \ 352 \ 344 \ 027 \ 374 | \ 273 \ 364XY,”

这同样适用于摘要命令;我错误地安装了pgcrypto吗?

postgresql output pgcrypto
1个回答
3
投票

return type of encryptbytea,可以用different ways表示。你的两个不同的输出只是两个相同值的表示(注意以“转义”格式的奇怪转义):

test=# select (bytea E'4Y\\026''\\371\\310\\352\\344\\027\\374|\\273\\364XY,') =
test-#        (bytea '\x34591627f9c8eae417fc7cbbf458592c') as eq;
 eq 
----
 t
(1 row)

在我安装PostgreSQL时,“hex”格式是默认格式,但由于这不适用于您,您可以设置bytea_output,它控制字节值的输出:

test=# select encrypt('123456789012345','1234','aes');
                   encrypt                   
---------------------------------------------
 4Y\026'\371\310\352\344\027\374|\273\364XY,
(1 row)

test=# set bytea_output = 'hex';
SET
test=# select encrypt('123456789012345','1234','aes');
              encrypt               
------------------------------------
 \x34591627f9c8eae417fc7cbbf458592c
(1 row)

您还可以显式编码字节以获取text值:

test=# select encode(encrypt('123456789012345','1234','aes'), 'hex');
              encode              
----------------------------------
 34591627f9c8eae417fc7cbbf458592c
(1 row)
© www.soinside.com 2019 - 2024. All rights reserved.