Skip to content

Commit

Permalink
Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Jones committed Sep 5, 2017
1 parent eded172 commit fc5dfcc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
4 changes: 2 additions & 2 deletions AzureSignTool/AuthenticodeKeyVaultSigner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ public int SignFile(string path, string description, string descriptionUrl)
ref signatureInfo,
IntPtr.Zero,
timeStampFlags,
timeStampFlags != 0 ? AlgorithmTranslator.HashAlgorithmToOid(_timeStampConfiguration.DigestAlgorithm) : null,
timeStampFlags != 0 ? _timeStampConfiguration.Url : null,
timestampAlgorithmOid,
timestampUrl,
IntPtr.Zero, IntPtr.Zero,
out signerContext,
IntPtr.Zero,
Expand Down
34 changes: 29 additions & 5 deletions AzureSignTool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static int Main(string[] args)
AzureClientId = azureKeyVaultClientId.Value(),
AzureAccessToken = azureKeyVaultAccessToken.Value(),
AzureClientSecret = azureKeyVaultClientSecret.Value(),
FileDigestAlgorithm = AlgorithmFromInput(fileDigestAlgorithm.Value()).GetValueOrDefault(HashAlgorithmName.SHA256)
FileDigestAlgorithm = GetValueFromOption(fileDigestAlgorithm, AlgorithmFromInput, HashAlgorithmName.SHA256)
};

var timestampConfiguration = new TimeStampConfiguration
Expand All @@ -62,16 +62,22 @@ static int Main(string[] args)
acTimeStamp.HasValue() ? acTimeStamp.Value() : null,
Type = rfc3161TimeStamp.HasValue() ? TimeStampType.RFC3161 :
acTimeStamp.HasValue() ? TimeStampType.Authenticode : TimeStampType.None,
DigestAlgorithm = rfc3161Digest.HasValue() ?
AlgorithmFromInput(rfc3161Digest.Value()).GetValueOrDefault(HashAlgorithmName.SHA256) :
HashAlgorithmName.SHA256
DigestAlgorithm = GetValueFromOption(rfc3161Digest, AlgorithmFromInput, HashAlgorithmName.SHA256)
};

using (var materialized = await KeyVaultConfigurationDiscoverer.Materialize(configuration))
using (var signer = new AuthenticodeKeyVaultSigner(materialized, timestampConfiguration))
{
const int S_OK = 0;
var result = signer.SignFile(file.Value, description.Value(), descriptionUrl.Value());
Console.WriteLine($"Signing completed as {result}.");
if (result == S_OK)
{
Console.WriteLine("Signing completed successfully.");
}
else
{
Console.WriteLine($"Signing failed with error {result}.");
}
return result;
}
});
Expand Down Expand Up @@ -128,5 +134,23 @@ private static bool CheckRequired(params CommandOption[] commands)
}
return true;
}

private static T GetValueFromOption<T>(CommandOption option, Func<string, T> transform, T defaultIfNull) where T:class
{
if (!option.HasValue())
{
return defaultIfNull;
}
return transform(option.Value()) ?? defaultIfNull;
}

private static T GetValueFromOption<T>(CommandOption option, Func<string, T?> transform, T defaultIfNull) where T:struct
{
if (!option.HasValue())
{
return defaultIfNull;
}
return transform(option.Value()) ?? defaultIfNull;
}
}
}

0 comments on commit fc5dfcc

Please sign in to comment.