从ruby中创建StandardPasswordEncoder密码哈希值

时间:2018-04-06 07:08:35

标签: ruby spring puppet

我们正在运行不同的java应用程序,它们使用org.springframework.security.crypto.password.StandardPasswordEncoder来存储和读取密码哈希,以保证所有用户密码的隐私。

我们通过puppet部署这些应用程序并将所有配置数据管理到hieradata(eyaml)。 现在我们要将所有密码存储在加密的yaml文件中,并在puppet中创建哈希值。 为此,我们想要创建一个puppet(ruby)函数来以正确的哈希格式创建密码哈希。

StandardPasswordEncoder似乎是用于加密密码的特殊spring类。有没有办法从ruby创建这样的哈希?

1 个答案:

答案 0 :(得分:0)

您可以使用Digest在ruby中创建哈希,它是一个核心库https://ruby-doc.org/stdlib-2.4.0/libdoc/digest/rdoc/Digest/SHA2.html

require 'digest'

# Compute a complete digest
Digest::SHA2.hexdigest 'abc'          # => "ba7816bf8..."
Digest::SHA2.new(256).hexdigest 'abc' # => "ba7816bf8..."
Digest::SHA256.hexdigest 'abc'        # => "ba7816bf8..."

Digest::SHA2.new(384).hexdigest 'abc' # => "cb00753f4..."
Digest::SHA384.hexdigest 'abc'        # => "cb00753f4..."

Digest::SHA2.new(512).hexdigest 'abc' # => "ddaf35a19..."
Digest::SHA512.hexdigest 'abc'        # => "ddaf35a19..."

# Compute digest by chunks
sha2 = Digest::SHA2.new               # =>#<Digest::SHA2:256>
sha2.update "ab"
sha2 << "c"                           # alias for #update
sha2.hexdigest                        # => "ba7816bf8..."

# Use the same object to compute another digest
sha2.reset
sha2 << "message"
sha2.hexdigest                        # => "ab530a13e..."