计算Android目录层次结构中文件的哈希值

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

我需要计算目录层次结构中文件的 md5 哈希值。我使用以下案例作为测试。我的 Android 设备有一个

md5
二进制文件,但需要文件的绝对路径 (
md5 <filename>
)。

我在 Android 设备上有以下目录层次结构:

/data/local/tmp/test1
/data/local/tmp/test1/test2
/data/local/tmp/test1/test2/test3

为了获取绝对路径列表,我遵循了here提到的答案。

$ adb shell 'function rcrls() { ls -d $1/* | while read f; do echo "$f"; if [ -d "$f" ]; then rcrls "$f"; fi; done } ; rcrls /data/local/tmp/' > filelist.txt

现在我有了每个文件的绝对路径列表。

接下来我想在脚本中逐行读取这个文件,并为每一行调用

md5
。如果输入是目录,
md5
将打印一条消息。我按照here的示例进行操作。

#! /bin/bash
filename='filelist.txt'
cat $filename | while read LINE; do
    adb shell 'md5 $LINE'
done

我得到以下输出:

/data/local/tmp/test1/test2
/data/local/tmp/test1/test2/test3
could not read /data/local/tmp/test1, Is a directory

我希望它打印 test1 和 test2 的目录警告,然后打印 test3 的 md5,因为 test3 是一个文件。有人可以建议如何修复此代码吗?我期待类似的事情:

could not read /data/local/tmp/test1, Is a directory
could not read /data/local/tmp/test1/test2, Is a directory
<hash_value>   /data/local/tmp/test1/test2/test3
android adb md5
2个回答
1
投票

你应该使用

find
:

adb shell find /data/local/tmp -type f -exec md5sum {} '\;'

0
投票

作为已接受解决方案的替代方案,也可以使用 hashdeep 或 hashdir。

对于哈希深;

apt install hashdeep # on android, termux supports apt by default 
hashdeep -rl .

对于哈希目录;

pip3 install hashdir
hashdir .

免责声明:我是 hashdir 的开发者 (https://pypi.org/project/hashdir/)

© www.soinside.com 2019 - 2024. All rights reserved.