public async Task <CdnRet> SyncFile(string dstPath) { // Get a file from CDN if i don't already have it and check its integrity (if supplied) // can throw. byte[] hashCode1 = null; HashUtil hasher = null; var fi = new FileInfo(dstPath); if (integrity == null) { // integrity doesn't exist so just check that the file exists locally. if (fi.Exists && fi.Length > 0) { await UpdateIntegrity(dstPath); // suggest that we manually update integrity. return(CdnRet.Valid); // it exists. good enough since we don't have integrity attribute. } // It doesn't exist locally. pull a local copy from CDN. No big deal. } else { // test local file integrity hash e.g. "sha256-", "sha384-" int i = integrity.IndexOf(kIntegritySep); if (i <= 0) // badly formed integrity ?? Fail ? { // This is bad !! i cant really fix this. fix it manually. LoggerUtil.DebugException($"Bad CDN integrity format '{integrity}'", null); return(CdnRet.Error); } hashCode1 = Convert.FromBase64String(integrity.Substring(i + 1)); hasher = new HashUtil(HashUtil.FindHasherByName(integrity)); if (fi.Exists) { // Does current file match integrity? byte[] hashCode2 = await hasher.GetHashFileAsync(dstPath); Debug.Assert(hashCode2 != null); // string debugHash2 = Convert.ToBase64String(hashCode2); if (ByteUtil.CompareBytes(hashCode1, hashCode2) == 0) // integrity match is good. Skip this. { return(CdnRet.Valid); } // local file DOES NOT MATCH integrity!! // This really should never happen! Pull another file from the CDN and hope it matches. } } if (this.CdnPath1 == null) { // this file has no CDN. So i can't do anything! if (Minifier.IsMinName(dstPath) && await Minifier.CreateMinified(Minifier.GetNonMinName(dstPath), dstPath)) { // Local only lacked a minified version. so i made one. return(CdnRet.Updated); } LoggerUtil.DebugException($"No File ({integrity}) and No CDN path for '{name}'", null); return(CdnRet.Error); } // Pull/Get the file from CDN. LoggerUtil.DebugEntry($"Get '{this.CdnPath1}'"); var dl = new HttpDownloader(this.CdnPath1, dstPath); // CDN can get "OperationCanceledException: The operation was canceled." await dl.DownloadFileAsync(true); // Assume directory is created on demand. if (integrity == null) { // destination file should exist now. Does it? var fi2 = new FileInfo(dstPath); if (!fi2.Exists || fi2.Length <= 0) { LoggerUtil.DebugException("CDN file size 0 for " + dstPath, null); return(CdnRet.Error); } await UpdateIntegrity(dstPath); } else { // Now (re)test integrity for the file i just got! hasher.Init(); byte[] hashCode2 = await hasher.GetHashFileAsync(dstPath); if (ByteUtil.CompareBytes(hashCode1, hashCode2) != 0) // MUST match. { // This is BAD. It should never happen! string debugHash2 = Convert.ToBase64String(hashCode2); LoggerUtil.DebugException($"CDN integrity hash does not match for '{dstPath}'. should be integrity='{string.Concat(kIntegrityAlgDef, kIntegritySep, debugHash2)}'", null); return(CdnRet.Error); } } return(CdnRet.Updated); // got it. }
public CdnResource(string _name, string _tagname, string outDir) { // create an element on the fly. Assume _name is a local minified file. NOT CDN. // <IncludeRef name="/js/grid_common.min.js" tagname="script" /> // use minified name to indicate it has one. ValidState.ThrowIfWhiteSpace(_name, "CdnResource name"); name = _name; fallback_src = name; if (_tagname == null) { // derive from the file extension. if (_name.EndsWith(Minifier.kExtJs)) { TagName = CdnTagName.script; } else if (_name.EndsWith(Minifier.kExtCss)) { TagName = CdnTagName.link; } else { TagName = CdnTagName.a; } } else { TagName = EnumUtil.ParseEnum <CdnTagName>(_tagname); } // Does it have a minified file? assume _name is the minified version. string path1 = GetPhysPathFromWeb(outDir, name); string name2 = Minifier.GetNonMinName(name); string path2 = GetPhysPathFromWeb(outDir, name2); bool exists1 = File.Exists(path1); bool exists2 = File.Exists(path2); if (exists1) { minonly = !exists2; // min exists. but does non-min ? } else if (exists2) { name = name2; // only non-min version exists. path1 = path2; minonly = true; } else { // neither exists. this is bad. TagName = CdnTagName.ERROR; minonly = false; return; } // cache breaking version For local files. Equiv to "asp-append-version" (would use SHA256) var hasher = new HashUtil(HashUtil.GetMD5()); // GetSHA256() byte[] hashCode2 = hasher.GetHashFile(path1); Debug.Assert(hashCode2 != null); version = Convert.ToBase64String(hashCode2); }
public byte[] GetHashSized(string password, string systemsecret, ulong salt, int id, int lenOutBin = 16) { // Make a arbitrarily sized hash for a password. for db storage. // lenOutBin = return size. return(HashUtil.MakeHashLen(GetHash(password, systemsecret, salt, id), lenOutBin)); }