GBase 8a新版本支持了sha256,本文介绍从算法角度,sha256和sha1的性能差距。
目录导航
结论
sha1和sha256从算法角度,性能下降在 20%。
复用UUID
generateStringToHash() with: return UUID_STRING
数据编码长度在 ~36 字符(f5cdcda7-d873-455f-9902-dc9c7894bee0). UUID复用了。
Hash | #1 (ms) | #2 (ms) | #3 (ms) | #4 (ms) | #5 (ms) | Average per 1M (ms) |
---|---|---|---|---|---|---|
MD5 | 649 | 623 | 621 | 624 | 620 | 627.4 |
SHA-1 | 608 | 588 | 630 | 600 | 594 | 604 |
SHA-256 | 746 | 724 | 741 | 720 | 758 | 737.8 |
SHA-512 | 1073 | 1055 | 1050 | 1052 | 1052 | 1056.4 |
复用UUID+时间戳
generateStringToHash() with: return UUID_STRING + System.currentTimeMillis();
数据编码长度 ~49 字符(aa096640-21d6-4f44-9c49-4115d3fa69381468217419114). 包括了时间戳。UUID复用了。
Hash | #1 (ms) | #2 (ms) | #3 (ms) | #4 (ms) | #5 (ms) | Average per 1M (ms) |
---|---|---|---|---|---|---|
MD5 | 751 | 789 | 745 | 806 | 737 | 765.6 |
SHA-1 | 768 | 765 | 694 | 763 | 751 | 748.2 |
SHA-256 | 842 | 876 | 848 | 839 | 850 | 851 |
SHA-512 | 1161 | 1152 | 1164 | 1154 | 1163 | 1158.8 |
计算UUID+时间戳
generateStringToHash() with: return UUID.randomUUID().toString() + System.currentTimeMillis();
字符编码长度 ~49(1af4a3e1-1d92-40e7-8a74-7bb7394211e01468216765464). 每次重新计算UUID.
Hash | #1 (ms) | #2 (ms) | #3 (ms) | #4 (ms) | #5 (ms) | Average per 1M (ms) |
---|---|---|---|---|---|---|
MD5 | 1505 | 1471 | 1518 | 1463 | 1487 | 1488.8 |
SHA-1 | 1333 | 1309 | 1323 | 1326 | 1334 | 1325 |
SHA-256 | 1505 | 1496 | 1507 | 1498 | 1516 | 1504.4 |
SHA-512 | 1834 | 1827 | 1833 | 1836 | 1857 | 1837.4 |
复用UUID+复用UUID
generateStringToHash() with: return UUID_STRING + UUID_STRING;
数据编码长度 ~72 字节(57149cb6-991c-4ffd-9c98-d823ee8a61f757149cb6-991c-4ffd-9c98-d823ee8a61f7)。
Hash | #1 (ms) | #2 (ms) | #3 (ms) | #4 (ms) | #5 (ms) | Average per 1M (ms) |
---|---|---|---|---|---|---|
MD5 | 856 | 824 | 876 | 811 | 828 | 839 |
SHA-1 | 921 | 896 | 970 | 904 | 893 | 916.8 |
SHA-256 | 1145 | 1137 | 1241 | 1141 | 1177 | 1168.2 |
SHA-512 | 1133 | 1131 | 1116 | 1102 | 1110 | 1118.4 |
复用UUID+复用UUID+时间戳
generateStringToHash() with: return UUID_STRING + UUID_STRING + System.currentTimeMillis();
数据编码长度 ~85字符 (759529c5-1f57-4167-b289-899c163c775e759529c5-1f57-4167-b289-899c163c775e1468218673060).
Hash | #1 (ms) | #2 (ms) | #3 (ms) | #4 (ms) | #5 (ms) | Average per 1M (ms) |
---|---|---|---|---|---|---|
MD5 | 1029 | 1035 | 1034 | 1012 | 1037 | 1029.4 |
SHA-1 | 1008 | 1016 | 1027 | 1007 | 990 | 1009.6 |
SHA-256 | 1254 | 1249 | 1290 | 1259 | 1248 | 1260 |
SHA-512 | 1228 | 1221 | 1232 | 1230 | 1226 | 1227.4 |
计算UUID+复用UUID+时间戳
generateStringToHash() with: final String randomUuid = UUID.randomUUID().toString();
return randomUuid + randomUuid + System.currentTimeMillis();
编码长度 ~85 字符(2734b31f-16db-4eba-afd5-121d0670ffa72734b31f-16db-4eba-afd5-121d0670ffa71468217683040). UUID每次重新产生,然后复用.
Hash | #1 (ms) | #2 (ms) | #3 (ms) | #4 (ms) | #5 (ms) | Average per 1M (ms) |
---|---|---|---|---|---|---|
MD5 | 1753 | 1757 | 1739 | 1751 | 1691 | 1738.2 |
SHA-1 | 1634 | 1634 | 1627 | 1634 | 1633 | 1632.4 |
SHA-256 | 1962 | 1956 | 1988 | 1988 | 1924 | 1963.6 |
SHA-512 | 1909 | 1946 | 1936 | 1929 | 1895 | 1923 |
参考
https://automationrhapsody.com/md5-sha-1-sha-256-sha-512-speed-performance/