gpt4 book ai didi

linux - 在 Bash 中使用密码短语散列字符串

转载 作者:太空宇宙 更新时间:2023-11-04 10:31:09 25 4
gpt4 key购买 nike

我需要一种使用密码(ASCII 字符串)对现有文件名进行散列处理的方法,但之后能够使用相同的密码将其还原。

我知道密码可以做到这一点——它们正在加密字符串……但它们的输出长度是基于文件名长度的,这正是我不想要的……主要是因为它有时会使文件长度加倍,但输出的字符串并不总是与 FS 兼容。前任。文件名中的“\n”。

明确地说,我做了很多研究,甚至写了一些脚本,但所有的解决方案要么很慢,要么根本不适合我的应用程序。

此操作的目标 是获得一个恒定长度的文件名,这些文件名可以使用单个密码一次性全部“解密”。无需创建额外的“类元数据”文件。

最佳答案

我已经解决了最初的问题。上面的问题似乎只有一种解决方案,那就是(正如 James 所建议的)格式保留加密。不过,据我所知,目前还没有执行此操作的命令。

所以我做了我的第一个选择,那就是对文件名进行哈希处理,将哈希和文件名放入一个普通文件(每个目录一个文件)并使用密码加密该文件。

我会在这里发布我的代码。虽然它可能不是最漂亮也不是最便携的代码,但它可以完成工作并且 (IMO) 非常简单。

#!/usr/bin/env bash

man="Usage: namecrypt [ -h ] [ -e || -d ] [ -F ] [ -D ] [DIRECTORY] [PASSPHRASE]\n
-h, --help display this message

-e, --encrypt encrypt the specified directory
-d, --decrypt decrypt the specified directory

-F, --files include files
-D, --dir include directories

[DIRECTORY] relative or absolute path to a directory/symbolic link
[PASSPHRASE] optional - specify the user passphrase on command line";

options=();
for Arg in "$@"; do
if [ "$Arg" == "-h" ] || [ "$Arg" == "--help" ]; then
echo -e "$man"; exit;
elif [ "$Arg" == "-e" ] || [ "$Arg" == "--encrypt" ]; then
options[0]="E";
elif [ "$Arg" == "-d" ] || [ "$Arg" == "--decrypt" ]; then
options[0]="D";
elif [ "$Arg" == "-F" ] || [ "$Arg" == "--files" ]; then
options[1]="${options[1]}F";
elif [ "$Arg" == "-D" ] || [ "$Arg" == "--dir" ]; then
options[1]="${options[1]}D";
elif [ -d "$Arg" ]; then
options[2]="$(realpath "$Arg")";
else
options[3]="$Arg";
fi;
done;

if [ "${options[0]}" == "" ]; then echo "No Mode specified!"; exit 1; fi;
if [ "${options[1]}" == "" ]; then options[1]="F"; fi;
if [ "${options[2]}" == "" ]; then echo "No such directory!"; exit 2; fi;
if [ "${options[3]}" == "" ]; then echo "Enter a passphrase: "; read options[3]; fi;

shopt -s nullglob dotglob;

function hashTarget
{
BASE="$(basename "$1")";
DIR="$(dirname "$1")/";

if [ -a "$1" ]; then
oldName="$BASE";
newName=$(echo "$oldName" | md5sum);
echo "$oldName||$newName" >> "$DIR.names";
mv "$1" "$DIR$newName";
else echo "Skipping '$1' - No such file or directory!";
fi;
}

function dehashTarget
{
BASE="$(basename "$1")";
DIR="$(dirname "$1")/";

[ -f "$DIR.names.cpt" ] && ccdecrypt -K "${options[3]}" "$DIR.names.cpt";

if [ -f "$DIR.names" ]; then
oldName="$BASE";
newName=$(grep "$oldName" "$DIR.names" | awk -F '|' '{print $1}');
[[ ! -z "${newName// }" ]] && mv "$1" "$DIR$newName";
else
echo "Skipping '$1' - Hash table not found!";
fi;
}

function mapTarget
{
DIR="$(dirname "$1")/";

for Dir in "$1"/*/; do
mapTarget "$Dir";
done;

for Item in "$1"/*; do
if ([ -f "$Item" ] && [[ "${options[1]}" == *"F"* ]]) ||
([ -d "$Item" ] && [[ "${options[1]}" == *"D"* ]]); then

if [ "${options[0]}" == "E" ]; then
hashTarget "$Item";
else
dehashTarget "$Item";
fi;
fi;
done;

[ "${options[0]}" == "D" ] && [ -f "$DIR.names" ] && rm "$DIR.names";
[ "${options[0]}" == "E" ] && [ -f "$DIR.names" ] && ccencrypt -K "${options[3]}" "$DIR.names";

}

mapTarget "${options[2]}";

可能它这么长的唯一原因是因为我没有为任何 OOP 烦恼,而且我还做了很多检查和步骤以确保大多数时候没有名字被破坏并且不能恢复 - 用户错误仍然可能导致此问题。

这是用于加密哈希表文件的命令:CCrypt

关于linux - 在 Bash 中使用密码短语散列字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39840471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com