HashCommand.cpp (2430B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #include "pch.h" 4 #include "HashCommand.h" 5 #include "Workflows/WorkflowBase.h" 6 #include "Resources.h" 7 8 #include <AppInstallerMsixInfo.h> 9 10 namespace AppInstaller::CLI 11 { 12 using namespace std::string_view_literals; 13 using namespace Utility::literals; 14 15 std::vector<Argument> HashCommand::GetArguments() const 16 { 17 return { 18 Argument::ForType(Execution::Args::Type::HashFile), 19 Argument::ForType(Execution::Args::Type::Msix), 20 }; 21 } 22 23 Resource::LocString HashCommand::ShortDescription() const 24 { 25 return { Resource::String::HashCommandShortDescription }; 26 } 27 28 Resource::LocString HashCommand::LongDescription() const 29 { 30 return { Resource::String::HashCommandLongDescription }; 31 } 32 33 Utility::LocIndView HashCommand::HelpLink() const 34 { 35 return "https://aka.ms/winget-command-hash"_liv; 36 } 37 38 void HashCommand::ExecuteInternal(Execution::Context& context) const 39 { 40 context << 41 Workflow::VerifyFile(Execution::Args::Type::HashFile) << 42 [](Execution::Context& context) 43 { 44 auto inputFile = context.Args.GetArg(Execution::Args::Type::HashFile); 45 std::ifstream inStream{ Utility::ConvertToUTF16(inputFile), std::ifstream::binary }; 46 47 context.Reporter.Info() << "InstallerSha256: "_liv << Utility::LocIndString{ Utility::SHA256::ConvertToString(Utility::SHA256::ComputeHash(inStream)) } << std::endl; 48 49 if (context.Args.Contains(Execution::Args::Type::Msix)) 50 { 51 try 52 { 53 Msix::MsixInfo msixInfo{ inputFile }; 54 auto signatureHash = msixInfo.GetSignatureHash(); 55 56 context.Reporter.Info() << "SignatureSha256: "_liv << Utility::LocIndString{ Utility::SHA256::ConvertToString(signatureHash) } << std::endl; 57 } 58 catch (const wil::ResultException& re) 59 { 60 context.Reporter.Warn() << 61 Resource::String::MsixSignatureHashFailed << std::endl << 62 Resource::String::VerifyFileSignedMsix << std::endl; 63 AICLI_TERMINATE_CONTEXT(re.GetErrorCode()); 64 } 65 } 66 }; 67 } 68 }