123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- namespace ArcToolkitCLI.Util
- {
- public static class Encryption
- {
- public static byte[] ReadKeyFromFile(string filename)
- {
- using (var br = new BinaryReader(File.OpenRead(filename)))
- {
- return br.ReadBytes(2048);
- }
- }
- public static int GetDecryptionKeys(IDecryptionOptions opts, out Dictionary<string, byte[]> keys)
- {
- keys = new Dictionary<string, byte[]>(StringComparer.InvariantCultureIgnoreCase);
- if (!string.IsNullOrWhiteSpace(opts.KeyFile))
- {
- if (!File.Exists(opts.KeyFile))
- return Errors.Error(Errors.ErrorCodes.KeyNotFound);
- foreach (var line in File.ReadAllLines(opts.KeyFile))
- {
- var parts = line.Split(':');
- if (parts.Length != 2)
- return Errors.Error(Errors.ErrorCodes.InvalidKeyFile);
- keys[parts[0].Trim()] = Convert.FromBase64String(parts[1].Trim());
- }
- }
- else if (!string.IsNullOrWhiteSpace(opts.DecryptionKey))
- {
- keys["*"] = Convert.FromBase64String(opts.DecryptionKey);
- }
- else if (string.IsNullOrWhiteSpace(opts.ArcDirectory) && string.IsNullOrWhiteSpace(opts.WarcFile))
- {
- return Errors.Error(Errors.ErrorCodes.KeyNotFound);
- }
- return 0;
- }
- }
- }
|