如何在MAC OS上安装sha1sum?

44
问题: 在MAC OS中获取文件的sha1sum值,我运行以下命令:
sha1sum file_name

我收到的错误信息如下:
perl version 5.18.2 can't run /usr/bin/sha1sum.  Try the alternative(s):

/usr/bin/sha1sum5.16 (uses perl 5.16)

Run "man perl" for more information about multiple version support in
Mac OS X.

我尝试安装textutils和coreutils,但都失败了。有人能建议我如何解决这个问题吗?谢谢。


1
为什么不将sha1sum别名设置为/usr/bin/sha1sum5.16呢? - prodigitalson
我正在从Python脚本中调用sha1sum,而不仅仅是尝试找到一个特定的文件。 - J4cK
我理解你的意思,但那真的有关系吗?我猜你只是在执行shell脚本/二进制文件,所以我的建议是使用符号链接或别名来执行你需要的二进制文件。显然,你想要的命令已经存在,只是因为安装的Perl版本和操作系统不同而被命名不同。此外,我很难相信Python没有获取sha1校验和的工具 - 为什么不使用它,这样你就可以减少外部依赖了呢? - prodigitalson
另外,关于安装方面,您尝试使用Macports或Homebrew了吗? - prodigitalson
1
我宁愿直接在Python代码中进行哈希处理,而不是调用外部程序。这需要写更多的代码,但可能更有效率。同时也可以避免外部依赖。请参考http://www.pythoncentral.io/hashing-files-with-python/。 - deceze
@prodigitalson,是的,我都试过Macports或Homebrew了。谢谢你的信息。 - J4cK
3个回答

60

1
对于我的使用情况,我在功能上看不出与本地shasum命令的区别。例如,我的echo -n“test”| shasum输出与echo -n“test”| sha1sum的输出完全相同。 - Chris Redford
1
是的,但如果您希望您的旧Linux脚本在MacOS上正常工作并继续在Linux上工作,那么这个解决方案是最好的。 - sscarduzio

43

MacOS 预装了 shasum 工具,它默认可以计算 sha1 校验和:

$ shasum --help
Usage: shasum [OPTION]... [FILE]...
Print or check SHA checksums.
With no FILE, or when FILE is -, read standard input.

  -a, --algorithm   1 (default), 224, 256, 384, 512, 512224, 512256
[...]

使用示例:

$ echo -n "test" | shasum
a94a8fe5ccb19ba61c4c0873d391e987982fbbd3  -

同样,openssl sha1也可以完成这个任务:

$ echo -n "test" | openssl sha1
a94a8fe5ccb19ba61c4c0873d391e987982fbbd3

2
两者都是空字符串的哈希,因为您的命令存在错误。正确的使用方法是回显字符串:echo -n "test" | openssl sha1 或 echo -n "test" | shasum - rezakamalifard
1
@itmard 你说得对!我已经更新了答案,谢谢! - DarthVanger

3
你可以使用shasum或openssl来计算哈希值。
echo -n "yourpassword" | openssl sha1
b48cf0140bea12734db05ebcdb012f1d265bed84


echo -n "yourpassword" | shasum
b48cf0140bea12734db05ebcdb012f1d265bed84

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接