public GetDerivedSystemSecretKeyResponse GetDerivedSystemSecretKey(GetDerivedSystemSecretKeyRequest request) { var store = SystemIdentityStore.Load(); using (var ms = new MemoryStream()) using (var hash = new SHA512CryptoServiceProvider2()) { var input = Encoding.UTF8.GetBytes(request.Input); ms.Write(input, 0, input.Length); ms.WriteByte(0); var secretKey = store.SecretKey; ms.Write(secretKey, 0, secretKey.Length); ms.Position = 0; var result = new string(BitConverter.ToString(hash.ComputeHash(ms)).Where(c => c != '-').Select(char.ToUpperInvariant).ToArray()); return(new GetDerivedSystemSecretKeyResponse { Key = result }); } }
internal static SystemIdentityStore Load() { if (_store != null) return _store; lock (_syncroot) { if (_store == null) { const string userName = "******"; var serializer = new XmlSerializer(typeof (SystemIdentityStore)); var documentName = typeof (SystemIdentityStore).FullName; var versionString = VersionUtils.ToPaddedVersionString(new Version(0, 0), false, false); var criteria = new ConfigurationDocumentSearchCriteria(); criteria.User.EqualTo(userName); criteria.DocumentName.EqualTo(documentName); criteria.DocumentVersionString.EqualTo(versionString); SystemIdentityStore store = null; using (var scope = new PersistenceScope(PersistenceContextType.Read)) { var broker = scope.Context.GetBroker<IConfigurationDocumentBroker>(); var document = broker.Find(criteria).FirstOrDefault(); if (document != null) { try { using (var reader = new StringReader(document.Body.DocumentText)) store = (SystemIdentityStore) serializer.Deserialize(reader); } catch (Exception) { store = null; } } scope.Complete(); } if (store == null || store.SecretKey == null || store.SecretKey.Length == 0) { if (store == null) store = new SystemIdentityStore(); store.SecretKey = new byte[128]; using (var crng = new RNGCryptoServiceProvider()) crng.GetBytes(store.SecretKey); using (var scope = new PersistenceScope(PersistenceContextType.Update)) using (var writer = new StringWriter()) { serializer.Serialize(writer, store); var broker = scope.Context.GetBroker<IConfigurationDocumentBroker>(); var document = broker.Find(criteria).FirstOrDefault(); if (document != null) { document.Body.DocumentText = writer.ToString(); } else { document = new ConfigurationDocument(documentName, versionString, userName, null); document.Body.DocumentText = writer.ToString(); scope.Context.Lock(document, DirtyState.New); } scope.Complete(); } } Interlocked.Exchange(ref _store, store); } return _store; } }
internal static SystemIdentityStore Load() { if (_store != null) { return(_store); } lock (_syncroot) { if (_store == null) { const string userName = "******"; var serializer = new XmlSerializer(typeof(SystemIdentityStore)); var documentName = typeof(SystemIdentityStore).FullName; var versionString = VersionUtils.ToPaddedVersionString(new Version(0, 0), false, false); var criteria = new ConfigurationDocumentSearchCriteria(); criteria.User.EqualTo(userName); criteria.DocumentName.EqualTo(documentName); criteria.DocumentVersionString.EqualTo(versionString); SystemIdentityStore store = null; using (var scope = new PersistenceScope(PersistenceContextType.Read)) { var broker = scope.Context.GetBroker <IConfigurationDocumentBroker>(); var document = broker.Find(criteria).FirstOrDefault(); if (document != null) { try { using (var reader = new StringReader(document.Body.DocumentText)) store = (SystemIdentityStore)serializer.Deserialize(reader); } catch (Exception) { store = null; } } scope.Complete(); } if (store == null || store.SecretKey == null || store.SecretKey.Length == 0) { if (store == null) { store = new SystemIdentityStore(); } store.SecretKey = new byte[128]; using (var crng = new RNGCryptoServiceProvider()) crng.GetBytes(store.SecretKey); using (var scope = new PersistenceScope(PersistenceContextType.Update)) using (var writer = new StringWriter()) { serializer.Serialize(writer, store); var broker = scope.Context.GetBroker <IConfigurationDocumentBroker>(); var document = broker.Find(criteria).FirstOrDefault(); if (document != null) { document.Body.DocumentText = writer.ToString(); } else { document = new ConfigurationDocument(documentName, versionString, userName, null); document.Body.DocumentText = writer.ToString(); scope.Context.Lock(document, DirtyState.New); } scope.Complete(); } } Interlocked.Exchange(ref _store, store); } return(_store); } }