Tuesday, September 21, 2010

Five button combination

So, let's say you have a five button combination lock, and you want to generate a list of values to manually crack said combination. How would you do it?

First, assume that for mnemonic reasons, the buttons are labelled with "1/2", "3/4", and so on. This means that for coding purposes, you don't have to account for every value, only the odd-numbered ones.

Also, assume that, as shown in the last SQL statement, you only need non-sequential results. Interestingly, this isn't going to be true in the real world, as a customer may need the ability to set the combination with a duplicate value to be more easily recalled, but it works for this blog post. :)


-- to reduce all the "Inserted 1 row" messages
set nocount on;

declare @i int = 1;
declare @j int = 1;
declare @k int = 1;
declare @g int = 1;
declare @h int = 1;
declare @b table
(
push1 int, push2 int, push3 int, push4 int, push5 int
)

while @g <= 9
begin
while @h <= 9
begin
while @i <= 9
begin
while @j <= 9
begin
while @k <= 9
begin
insert into @b select @g, @h, @i, @j, @k
set @k += 2;
end
set @k = 1;
set @j += 2;
end
set @k = 1;
set @j = 1;
set @i += 2;
end
set @k = 1;
set @j = 1;
set @i = 1 ;
set @h += 2;
end
set @k = 1;
set @j = 1;
set @i = 1;
set @h = 1;
set @g += 2;
end

-- If you want to see everything, you can uncomment this . . .
-- select count(*) as raw_count from @b;

-- To elimate 1-1-1-1-1 and 3-3-3-5-5
select *
from @b
where push1 <> push2
and push2 <> push3
and push3 <> push4
and push4 <> push5;