MS.StrongNameKeyFile.cs

  1  using System;  2  using System.Diagnostics;  3  using System.Globalization;  4  using System.IO;  5  using System.Security;  6    7  namespace MS.StrongName  8  {  9       /// <summary>10       ///      KeyFile I/O methods11       /// </summary>12      public static class KeyFile13      {14           /// <summary>15           ///      Read a key out of a .snk file and into a byte array16           /// </summary>17           /// <exception cref="ArgumentNullException">18           ///      If <paramref name="fileName"/> is null19           /// </exception>20           /// <exception cref="InvalidOperationException">21           ///      If the file could not be read22           /// </exception>23           /// <param name="fileName">Name of the file to read out of</param>24           /// <returns>true on success, false on error</returns>25          public static byte[] ReadKeyFile(string fileName)26          {27              if(fileName == null)28                  throw new ArgumentNullException("fileName");29              30              byte[] keyBlob = null;31  32              try33              {34                  using(FileStream snkStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))35                  {36                       // make sure the file isn't too big to read37                      if(snkStream.Length > Int32.MaxValue)38                          throw new InvalidOperationException(String.Format(39                                      CultureInfo.CurrentCulture,40                                      Resources.BadFile,41                                      fileName));42  43                      using(BinaryReader snkReader = new BinaryReader(snkStream))44                          keyBlob = snkReader.ReadBytes((int)snkStream.Length); // safe cast due to check above45                  }46              }47              catch(FileNotFoundException e)48              {49                  throw new InvalidOperationException(String.Format(50                      CultureInfo.CurrentCulture,51                      Resources.BadFile,52                      fileName), e);53              }54              catch(DirectoryNotFoundException e)55              {56                  throw new InvalidOperationException(String.Format(57                      CultureInfo.CurrentCulture,58                      Resources.BadFile,59                      fileName), e);60              }61              catch(IOException e)62              {63                  throw new InvalidOperationException(String.Format(64                      CultureInfo.CurrentCulture,65                      Resources.BadFile,66                      fileName), e);67              }68              catch(ArgumentException e)69              {70                  throw new InvalidOperationException(String.Format(71                      CultureInfo.CurrentCulture,72                      Resources.BadOperation,73                      e.Message), e);74              }75              catch(SecurityException e)76              {77                  throw new InvalidOperationException(String.Format(78                      CultureInfo.CurrentCulture,79                      Resources.BadOperation,80                      e.Message), e);81              }82              catch(UnauthorizedAccessException e)83              {84                  throw new InvalidOperationException(String.Format(85                      CultureInfo.CurrentCulture,86                      Resources.BadOperation,87                      e.Message), e);88              }89  90              Debug.Assert(keyBlob != null);91              return keyBlob;92          }93          94           /// <summary>95           ///      Write a key to a file96           /// </summary>97           /// <exception cref="ArgumentNullException">98           ///      If <paramref name="keyBlob"/> or <paramref name="fileName"/> are null99           /// </exception>100           /// <exception cref="InvalidOperationException">101           ///      If the file could not be written102           /// </exception>103           /// <param name="keyBlob">Key to write to the file</param>104           /// <param name="fileName">File to write into</param>105          public static void WriteKeyFile(byte[] keyBlob, string fileName)106          {107              if(keyBlob == null)108                  throw new ArgumentNullException("keyBlob");109              if(fileName == null)110                  throw new ArgumentNullException("fileName");111              112              try113              {114                   // write the key to the specified file115                  using(FileStream snkStream = new FileStream(fileName, FileMode.Create, FileAccess.Write))116                  using(BinaryWriter snkWriter = new BinaryWriter(snkStream))117                      snkWriter.Write(keyBlob);118              }119              catch(PathTooLongException e)120              {121                  throw new InvalidOperationException(String.Format(122                      CultureInfo.CurrentCulture,123                      Resources.BadOperation,124                      e.Message), e);125              }126              catch(UnauthorizedAccessException e)127              {128                  throw new InvalidOperationException(String.Format(129                      CultureInfo.CurrentCulture,130                      Resources.BadOperation,131                      e.Message), e);132              }133              catch(ArgumentException e)134              {135                  throw new InvalidOperationException(String.Format(136                      CultureInfo.CurrentCulture,137                      Resources.BadOperation,138                      e.Message), e);139              }140              catch(IOException e)141              {142                  throw new InvalidOperationException(String.Format(143                      CultureInfo.CurrentCulture,144                      Resources.BadOperation,145                      e.Message), e);146              }147  148              return;149          }150      }151  }