Skip to content

Commit

Permalink
write private key
Browse files Browse the repository at this point in the history
  • Loading branch information
jshawl committed Feb 7, 2024
1 parent 0f363f1 commit e9d5804
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
23 changes: 16 additions & 7 deletions lib/minisign/private_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ def initialize(str, password = nil)
@kdf_salt = bytes[6..37]
@kdf_opslimit = bytes[38..45].pack('V*').unpack('N*').sum
@kdf_memlimit = bytes[46..53].pack('V*').unpack('N*').sum
key_data_bytes = if password
kdf_output = derive_key(password, @kdf_salt, @kdf_opslimit, @kdf_memlimit)
xor(kdf_output, bytes[54..157])
else
bytes[54..157]
end
@key_id, @secret_key, @public_key, @checksum = key_data(key_data_bytes)
@key_data_bytes = if password
kdf_output = derive_key(password, @kdf_salt, @kdf_opslimit, @kdf_memlimit)
xor(kdf_output, bytes[54..157])
else
bytes[54..157]
end
@key_id, @secret_key, @public_key, @checksum = key_data(@key_data_bytes)
assert_keypair_match!
end
# rubocop:enable Layout/LineLength
Expand Down Expand Up @@ -91,5 +91,14 @@ def sign(filename, message, comment = nil)
''
].join("\n")
end

def to_s
kdf_algorithm = @password.nil? ? [0, 0].pack('U*') : 'Sc'
kdf_salt = @kdf_salt.pack('C*')
kdf_opslimit = [@kdf_opslimit, 0].pack('L*')
kdf_memlimit = [@kdf_memlimit, 0].pack('L*')
data = "Ed#{kdf_algorithm}B2#{kdf_algorithm}#{kdf_salt}#{kdf_opslimit}#{kdf_memlimit}#{@key_data_bytes}"
"untrusted comment: <arbitrary text>\n#{Base64.strict_encode64(data)}\n"
end
end
end
6 changes: 6 additions & 0 deletions spec/minisign/private_key_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@
computed_checksum = blake2b256(key_data).bytes
expect(@private_key.checksum).to eq(computed_checksum)
end

it 'can be written to a file' do
expect do
Base64.strict_decode64(@private_key.to_s.split("\n").last)
end.not_to raise_error
end
end

describe 'sign' do
Expand Down

0 comments on commit e9d5804

Please sign in to comment.