为保持R同mysql的MD5加密结果一致性,需要将digest函数中设置serialize = F。
mysql对数字加密会自动先转换为字符串,再进行MD5加密。但在R中需要先手动转为字符串再进行MD5加密。
# mysql
> que = "
+ select 'ab1' as a,md5('ab1') as md5_a,1.1 as num,md5(1.1) as md5_1
+ "
# R connect mysql
> tmp = connect_db_and_query(que)
Warning message:
In .local(conn, statement, ...) :
Decimal MySQL column 2 imported as numeric
> tmp
a md5_a num md5_1
1 ab1 68b6a776378decbb4a79cda89087c4ce 1.1000000000000001 777d45bbbcdf50d49c42c70ad7acf5fe
# 比较结果(serialize = F 与mysql加密结果一致)
> digest::digest(tmp$a,'md5',serialize = F) == tmp$md5_a
[1] TRUE
# R中数字加密,需要先转成字符串,serialize = F与mysql一致
> digest::digest('1.1','md5',serialize = F)
[1] "777d45bbbcdf50d49c42c70ad7acf5fe"
网友评论