public string DecompressCommand(Block block, TccOption option) { var cmd = new StringBuilder(); switch (option.PasswordOption.PasswordMode) { case PasswordMode.None: //lz4 archive.tar.lz4 -dc --no-sparse | tar xf - switch (block.Algo) { case CompressionAlgo.Lz4: cmd.Append($"{_ext.Lz4()} {block.Source} -dc --no-sparse "); break; case CompressionAlgo.Brotli: cmd.Append($"{_ext.Brotli()} {block.Source} -d -c "); break; case CompressionAlgo.Zstd: cmd.Append($"{_ext.Zstd()} {block.Source} -d -c "); break; default: throw new ArgumentOutOfRangeException(nameof(block), "Unknown PasswordMode"); } cmd.Append($" | {_ext.Tar()} xf - "); break; case PasswordMode.InlinePassword: case PasswordMode.PasswordFile: case PasswordMode.PublicKey: string passwdCommand = PasswordCommand(option, block); //openssl aes-256-cbc -d -k "test" -in crypted.tar.lz4.aes | lz4 -dc --no-sparse - | tar xf - cmd.Append($"{_ext.OpenSsl()} aes-256-cbc -d {passwdCommand} -in {block.Source}"); switch (block.Algo) { case CompressionAlgo.Lz4: cmd.Append($" | {_ext.Lz4()} -dc --no-sparse - "); break; case CompressionAlgo.Brotli: cmd.Append($" | {_ext.Brotli()} - -d "); break; case CompressionAlgo.Zstd: cmd.Append($" | {_ext.Zstd()} - -d "); break; default: throw new ArgumentOutOfRangeException(nameof(block), "Unknown PasswordMode"); } cmd.Append($" | {_ext.Tar()} xf - "); break; default: throw new ArgumentOutOfRangeException(nameof(option)); } return(cmd.ToString()); }
private static void ExtractPasswordInfo(BaseCmdOptions opts, TccOption option, Mode mode) { if (!String.IsNullOrEmpty(opts.Password)) { ExtractInlinePassword(opts, option); } if (!String.IsNullOrEmpty(opts.PasswordFile)) { ExtractPasswordFile(opts, option); } if (!String.IsNullOrEmpty(opts.PasswordKey)) { ExtractAsymetricFile(opts, option, mode); } }
private static void ExtractInlinePassword(BaseCmdOptions opts, TccOption option) { if (option.PasswordOption != NoPasswordOption.Nop) { throw new CommandLineException("Only one password mode allowed"); } if (string.IsNullOrEmpty(opts.Password)) { throw new CommandLineException("Password must be specified"); } option.PasswordOption = new InlinePasswordOption { Password = opts.Password }; }
private static void ExtractPasswordFile(BaseCmdOptions opts, TccOption option) { if (option.PasswordOption != NoPasswordOption.Nop) { throw new CommandLineException("Only one password mode allowed"); } if (string.IsNullOrEmpty(opts.PasswordFile)) { throw new CommandLineException("Password file must be specified"); } if (!File.Exists(opts.PasswordFile)) { throw new CommandLineException("Password file doesn't exists"); } option.PasswordOption = new PasswordFileOption { PasswordFile = opts.PasswordFile }; }
private static void ExtractAsymetricFile(BaseCmdOptions opts, TccOption option, Mode mode) { if (option.PasswordOption != NoPasswordOption.Nop) { throw new CommandLineException("Only one password mode allowed"); } if (string.IsNullOrEmpty(opts.PasswordKey)) { throw new CommandLineException("Public or private key must be specified"); } if (!File.Exists(opts.PasswordKey)) { throw new CommandLineException("Public or private key file doesn't exists"); } switch (mode) { case Mode.Compress: option.PasswordOption = new PublicKeyPasswordOption { PublicKeyFile = opts.PasswordKey }; break; case Mode.Decompress: option.PasswordOption = new PrivateKeyPasswordOption { PrivateKeyFile = opts.PasswordKey }; break; default: throw new ArgumentOutOfRangeException(nameof(mode), mode, null); } }
private static string PasswordCommand(TccOption option, Block block) { string passwdCommand; if (option.PasswordOption.PasswordMode == PasswordMode.InlinePassword && option.PasswordOption is InlinePasswordOption inlinePassword) { if (String.IsNullOrWhiteSpace(inlinePassword.Password)) { throw new CommandLineException("Password missing"); } passwdCommand = "-k " + inlinePassword.Password; } else { if (String.IsNullOrWhiteSpace(block.BlockPasswordFile)) { throw new CommandLineException("Password file missing"); } passwdCommand = "-kfile " + block.BlockPasswordFile.Escape(); } return(passwdCommand); }