public void DoubleHash_WorksWithIntermediateValue()
        {
            var guid = Guid.NewGuid();

            var raw        = guid.ToString();
            var single     = SelectByHashedProperty.HashForDatabaseValue(raw);
            var doubleHash = Convert.ToBase64String(SHA256.HashData(Convert.FromBase64String(single)));

            Assert.Equal(Convert.ToBase64String(SHA256.HashData(SHA256.HashData(Encoding.UTF8.GetBytes(raw)))),
                         doubleHash);

            // For some reason this doesn't result in the right values, even though the roundtrip as bytes in a string
            // should work
            // Assert.Equal(doubleHash,
            //     SelectByHashedProperty.HashForDatabaseValue(Encoding.UTF8.GetString(Convert.FromBase64String(single))));

            var tempQuery = Convert.FromBase64String(doubleHash).Take(sizeof(long));

            if (BitConverter.IsLittleEndian)
            {
                tempQuery = tempQuery.Reverse();
            }

            var doubleLong = BitConverter.ToInt64(tempQuery.ToArray(), 0);

            Assert.Equal(SelectByHashedProperty.DoubleHashAsIdStandIn(raw, single),
                         SelectByHashedProperty.DoubleHashAsIdStandIn(raw, null));
            Assert.Equal(doubleLong, SelectByHashedProperty.DoubleHashAsIdStandIn(raw, null));
            Assert.Equal(doubleLong, SelectByHashedProperty.DoubleHashAsIdStandIn(raw, single));
            Assert.Throws <ArgumentNullException>(() => SelectByHashedProperty.DoubleHashAsIdStandIn(null !, null));
            SelectByHashedProperty.DoubleHashAsIdStandIn(null !, single);
        }
コード例 #2
0
        public async Task RecomputeHashedColumns_ComputesSessionIdHash()
        {
            var database = fixture.Database;

            await using var transaction = await database.Database.BeginTransactionAsync();

            var created = new Session()
            {
                SsoNonce = "5123"
            };

            // It should be impossible to create a session with no hash, so we use raw SQL to insert it
            // It seems impossible to split the interpolated string here into multiple lines...
            var changes =
                await database.Database.ExecuteSqlInterpolatedAsync(
                    $"INSERT INTO sessions (id, session_version, last_used, sso_nonce) VALUES ({created.Id}, 1, {created.LastUsed}, {created.SsoNonce});");

            Assert.Equal(1, changes);

            created.ComputeHashedLookUpValues();
            Assert.Equal(SelectByHashedProperty.HashForDatabaseValue(created.Id.ToString()), created.HashedId);

            // Run the job
            var job = new RecomputeHashedColumns(logger, database);
            await job.Execute(default);
コード例 #3
0
        public async Task User_RecreatingLinkCodeUpdatesHashedVersion()
        {
            var database = new EditableInMemoryDatabaseFixture("UserLinkCodeRecreated");

            var user = new User()
            {
                UserName = "******",
                Email    = "*****@*****.**",
            };

            await database.Database.Users.AddAsync(user);

            await database.Database.SaveChangesAsync();

            Assert.Null(user.LauncherLinkCode);
            Assert.Null(user.HashedLauncherLinkCode);

            var firstGuid = Guid.NewGuid().ToString();

            user.LauncherLinkCode = firstGuid;
            await database.Database.SaveChangesAsync();

            Assert.NotNull(user.LauncherLinkCode);
            Assert.NotNull(user.HashedLauncherLinkCode);
            Assert.Equal(SelectByHashedProperty.HashForDatabaseValue(firstGuid), user.HashedLauncherLinkCode);

            // Setting a second code
            var secondGuid = Guid.NewGuid().ToString();

            Assert.NotEqual(secondGuid, firstGuid);

            user.LauncherLinkCode = secondGuid;
            await database.Database.SaveChangesAsync();

            Assert.Equal(SelectByHashedProperty.HashForDatabaseValue(secondGuid), user.HashedLauncherLinkCode);

            // Clearing the code
            user.LauncherLinkCode = null;

            await database.Database.SaveChangesAsync();

            Assert.Null(user.LauncherLinkCode);
            Assert.Null(user.HashedLauncherLinkCode);
        }
コード例 #4
0
        public async Task Session_AutomaticallyCreatesHashedIdOnSave()
        {
            var database = new EditableInMemoryDatabaseFixture("SessionCreateNewHash");

            var session = new Session()
            {
                SsoNonce = "1234"
            };

            await database.Database.Sessions.AddAsync(session);

            await database.Database.SaveChangesAsync();

            Assert.NotNull(session.HashedId);
            Assert.Equal(SelectByHashedProperty.HashForDatabaseValue(session.Id.ToString()), session.HashedId);

            var searched = await database.Database.Sessions.FirstAsync();

            Assert.Equal(session.Id, searched.Id);
            Assert.Equal(session.SsoNonce, searched.SsoNonce);
        }