使用bash将bcrypt哈希值插入文件的特定行中

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

我需要通过在特定行将bcrypt哈希插入文件config.yml中来更新应用程序配置。

echo -e "$Enter password for user1"
read -p ": " user1_pass

echo -e "$Enter password for user2"
read -p ": " user2_pass

user1_hash=$(htpasswd -bnBC 10 "" $user1_pass | tr -d ':\n')
user2_hash=$(htpasswd -bnBC 10 "" $user2_pass | tr -d ':\n')

[$user1_hash应该放在第2行中,$user2_hash应该放在第4行中。

config.yml

user1:
  hash: "$2y$12$shEKzuVfogdZFbbraSqhwOOh96hfxe1NzLQbpmHJvgDUeRfRrkf3a"
user2:
  hash: "$2y$12$Fkc5GAp9Za5caIfHjBgNQ.jNEss0SJfCLTlm9EhAcjzPVy.kLriBa"

使用bash的最佳方法是什么?

bash file insert bcrypt
1个回答
0
投票

您可以使用Ruby编辑文件:

#!/usr/bin/ruby

require 'yaml'
obj = YAML.load_file('config.yml')

bcrypt_hash='$2y$12$shEKzuVfogdZFbbraSqhwOOh96hfxe1NzLQbpmHJvgDUeRfRrkf3a' 
obj['user1'] = {"hash" => bcrypt_hash}
obj['user2'] = {"hash" => bcrypt_hash}

puts YAML.dump(obj)

这将输出:

... More yml content
user1:
  hash: "$2y$12$shEKzuVfogdZFbbraSqhwOOh96hfxe1NzLQbpmHJvgDUeRfRrkf3a"
user2:
  hash: "$2y$12$shEKzuVfogdZFbbraSqhwOOh96hfxe1NzLQbpmHJvgDUeRfRrkf3a"
... More yml content

希望有帮助!

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