/// <summary>
        /// We want to base the lock name off of the full path of the package, however, the Mutex looks for files on disk if a path is given.
        /// Additionally, it also fails if the string is longer than 256 characters. Therefore we obtain a base-64 encoded hash of the path.
        /// </summary>
        /// <seealso cref="http://social.msdn.microsoft.com/forums/en-us/clr/thread/D0B3BF82-4D23-47C8-8706-CC847157AC81"/>
        private static string GenerateUniqueToken(IPackageManager packageManager, string packageId, SemanticVersion version)
        {
            var packagePath  = packageManager.FileSystem.GetFullPath(packageManager.PathResolver.GetPackageFileName(packageId, version));
            var pathBytes    = Encoding.UTF8.GetBytes(packagePath);
            var hashProvider = new CryptoHashProvider("SHA256");

            return(Convert.ToBase64String(hashProvider.CalculateHash(pathBytes)).ToUpperInvariant());
        }
Example #2
0
        public static string GenerateUniqueToken(string caseInsensitiveKey)
        {
            if (string.IsNullOrEmpty(caseInsensitiveKey))
            {
                throw new ArgumentNullException(nameof(caseInsensitiveKey));
            }

            // SHA256 is case sensitive; given that our key is case insensitive, we upper case it
            var pathBytes    = Encoding.UTF8.GetBytes(caseInsensitiveKey.ToUpperInvariant());
            var hashProvider = new CryptoHashProvider("SHA256");

            return(Convert.ToBase64String(hashProvider.CalculateHash(pathBytes)).ToUpperInvariant());
        }