// File: wangyu-[P]pwdgen/main.cpp
// Function: main
int main(int argc, char *argv[])
{
	if(argc!=3&&argc!=4&&argc!=5)
	{
		printf("usage:\n");
		printf(" ./this_program pwd info\n");
		printf(" ./this_program pwd info len\n");
		printf(" ./this_program pwd info len custom_char_list\n");
		exit(-1);
	}

	sscanf(argv[1],"%s",user_passwd);
	sscanf(argv[2],"%s",hkdf_info);
	if(argc>=4)
		sscanf(argv[3],"%d",&output_len);
	if(argc>=5)
		sscanf(argv[4],"%s",custom_str);

	printf("pwd=[%s]\n",user_passwd);
	printf("---------\n");

	printf("info=[%s] len=[%d] str=[%s]\n",hkdf_info,output_len,custom_str);

	int user_passwd_len=strlen(user_passwd);

	unsigned char salt[1000]="";
	char salt_text[1000]="wangyu_personal_salt1";
	md5((uint8_t*)(salt_text),strlen(salt_text),salt);

	unsigned char pbkdf2_output1[1000]="";
	PKCS5_PBKDF2_HMAC_SHA256((uint8_t*)user_passwd,user_passwd_len,salt,16,it_count, 32,pbkdf2_output1);  //TODO argon2 ?

	assert( hkdf_sha256_expand( pbkdf2_output1,32, (unsigned char *)hkdf_info,strlen(hkdf_info), (unsigned char *)inner_output_buf, inner_output_len )  ==0);

	init_table();

	string output="";


	printf("---------\n");

	for(int i=0;i<inner_output_len;i++)
	{
		unsigned char c=inner_output_buf[i];
		//if(char_table[c]) printf("%c",char(c));
		if(char_table[c]) output+=char(c);
	}
	assert(int(output.length())>=output_len);

	for(int i=0;i<output_len;i++)
	{
		printf("%c",output[i]);
	}
	printf("\n");

	printf("---------\n");

	for(int i=0;i<output_len;i++)
	{
		if(i!=0&&i%5==0) printf(" ");
		printf("%c",output[i]);
	}
	printf("\n");
	return 0;
}

// Assembly:
subq $0xc68, %rsp            # imm = 0xC68
movl $0x0, 0xc64(%rsp)
movl %edi, 0xc60(%rsp)
movq %rsi, 0xc58(%rsp)
cmpl $0x3, 0xc60(%rsp)
je 0x402401
cmpl $0x4, 0xc60(%rsp)
je 0x402401
cmpl $0x5, 0xc60(%rsp)
je 0x402401
leaq 0xd8c3e(%rip), %rdi     # 0x4db004
movb $0x0, %al
callq 0x435910
leaq 0xd8c38(%rip), %rdi     # 0x4db00c
movb $0x0, %al
callq 0x435910
leaq 0xd8c44(%rip), %rdi     # 0x4db026
movb $0x0, %al
callq 0x435910
leaq 0xd8c54(%rip), %rdi     # 0x4db044
movb $0x0, %al
callq 0x435910
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x434de0
movq 0xc58(%rsp), %rax
movq 0x8(%rax), %rdi
leaq 0xf765d(%rip), %rsi     # 0x4f9a71
leaq 0x114cd5(%rip), %rdx    # 0x5170f0
movb $0x0, %al
callq 0x435aa0
movq 0xc58(%rsp), %rax
movq 0x10(%rax), %rdi
leaq 0xf763c(%rip), %rsi     # 0x4f9a71
leaq 0x1150a4(%rip), %rdx    # 0x5174e0
movb $0x0, %al
callq 0x435aa0
cmpl $0x4, 0xc60(%rsp)
jl 0x40246e
movq 0xc58(%rsp), %rax
movq 0x18(%rax), %rdi
leaq 0xdf011(%rip), %rsi     # 0x4e1471
leaq 0x115851(%rip), %rdx    # 0x517cb8
movb $0x0, %al
callq 0x435aa0
cmpl $0x5, 0xc60(%rsp)
jl 0x402499
movq 0xc58(%rsp), %rax
movq 0x20(%rax), %rdi
leaq 0xf75e6(%rip), %rsi     # 0x4f9a71
leaq 0x11543e(%rip), %rdx    # 0x5178d0
movb $0x0, %al
callq 0x435aa0
leaq 0xd8bd3(%rip), %rdi     # 0x4db073
leaq 0x114c49(%rip), %rsi    # 0x5170f0
movb $0x0, %al
callq 0x435910
leaq 0xd8bc8(%rip), %rdi     # 0x4db07d
movb $0x0, %al
callq 0x435910
movl 0x1157f6(%rip), %edx    # 0x517cb8
leaq 0xd8bbf(%rip), %rdi     # 0x4db088
leaq 0x115010(%rip), %rsi    # 0x5174e0
leaq 0x1153f9(%rip), %rcx    # 0x5178d0
movb $0x0, %al
callq 0x435910
leaq 0x114c0b(%rip), %rdi    # 0x5170f0
callq 0x4010d8
movl %eax, 0xc54(%rsp)
leaq 0x860(%rsp), %rdi
xorl %esi, %esi
movl $0x3e8, %edx            # imm = 0x3E8
callq 0x401098
leaq 0x470(%rsp), %rdi
leaq 0xd8c6c(%rip), %rsi     # 0x4db180
movl $0x3e8, %edx            # imm = 0x3E8
callq 0x401040
leaq 0x470(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq 0x470(%rsp), %rdi
callq 0x4010d8
movq 0x28(%rsp), %rdi
movq %rax, %rsi
leaq 0x860(%rsp), %rdx
callq 0x404080
leaq 0x80(%rsp), %rdi
xorl %esi, %esi
movl $0x3e8, %edx            # imm = 0x3E8
callq 0x401098
movslq 0xc54(%rsp), %rsi
leaq 0x860(%rsp), %rdx
leaq 0x80(%rsp), %rax
leaq 0x114b70(%rip), %rdi    # 0x5170f0
movl $0x10, %ecx
movl $0x186a0, %r8d          # imm = 0x186A0
movl $0x20, %r9d
movq %rax, (%rsp)
callq 0x4111e0
leaq 0x80(%rsp), %rax
movq %rax, 0x30(%rsp)
leaq 0x114f32(%rip), %rdi    # 0x5174e0
callq 0x4010d8
movq 0x30(%rsp), %rdi
movq %rax, %rcx
movl $0x20, %esi
leaq 0x114f19(%rip), %rdx    # 0x5174e0
leaq 0x1179c2(%rip), %r8     # 0x519f90
movl $0x1fe0, %r9d           # imm = 0x1FE0
callq 0x411570
cmpl $0x0, %eax
jne 0x4025e0
jmp 0x4025ff
leaq 0xd8abe(%rip), %rdi     # 0x4db0a5
leaq 0xd8b46(%rip), %rsi     # 0x4db134
movl $0x46, %edx
leaq 0xd8b43(%rip), %rcx     # 0x4db13d
callq 0x42e720
callq 0x402320
leaq 0x5f(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0x412700
movq 0x20(%rsp), %rdx
leaq 0xf635e(%rip), %rsi     # 0x4f897d
leaq 0x60(%rsp), %rdi
callq 0x415950
jmp 0x40262b
leaq 0x5f(%rsp), %rdi
callq 0x412720
leaq 0xd8a41(%rip), %rdi     # 0x4db07d
xorl %eax, %eax
callq 0x435910
jmp 0x402645
movl $0x0, 0x48(%rsp)
cmpl $0x1fe0, 0x48(%rsp)     # imm = 0x1FE0
jge 0x4026e1
movslq 0x48(%rsp), %rcx
leaq 0x117929(%rip), %rax    # 0x519f90
movb (%rax,%rcx), %al
movb %al, 0x47(%rsp)
movzbl 0x47(%rsp), %eax
movl %eax, %ecx
leaq 0x119964(%rip), %rax    # 0x51bfe0
testb $0x1, (%rax,%rcx)
je 0x4026cf
movsbl 0x47(%rsp), %esi
leaq 0x60(%rsp), %rdi
callq 0x413530
jmp 0x402693
jmp 0x4026cf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x50(%rsp)
movl %eax, 0x4c(%rsp)
leaq 0x5f(%rsp), %rdi
callq 0x412720
jmp 0x402834
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x50(%rsp)
movl %eax, 0x4c(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x412f20
jmp 0x402834
jmp 0x4026d1
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0x40264d
leaq 0x60(%rsp), %rdi
callq 0x413150
cmpl 0x1155c7(%rip), %eax    # 0x517cb8
jl 0x4026f5
jmp 0x402714
leaq 0xd8a58(%rip), %rdi     # 0x4db154
leaq 0xd8a31(%rip), %rsi     # 0x4db134
movl $0x55, %edx
leaq 0xd8a2e(%rip), %rcx     # 0x4db13d
callq 0x42e720
movl $0x0, 0x40(%rsp)
movl 0x40(%rsp), %eax
cmpl 0x115592(%rip), %eax    # 0x517cb8
jge 0x402765
movslq 0x40(%rsp), %rsi
leaq 0x60(%rsp), %rdi
callq 0x413460
movq %rax, 0x18(%rsp)
jmp 0x40273e
movq 0x18(%rsp), %rax
movsbl (%rax), %esi
leaq 0xd8a28(%rip), %rdi     # 0x4db175
xorl %eax, %eax
callq 0x435910
jmp 0x402756
jmp 0x402758
movl 0x40(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
jmp 0x40271c
leaq 0xdae1e(%rip), %rdi     # 0x4dd58a
xorl %eax, %eax
callq 0x435910
jmp 0x402775
leaq 0xd8901(%rip), %rdi     # 0x4db07d
xorl %eax, %eax
callq 0x435910
jmp 0x402785
movl $0x0, 0x3c(%rsp)
movl 0x3c(%rsp), %eax
cmpl 0x115521(%rip), %eax    # 0x517cb8
jge 0x402800
cmpl $0x0, 0x3c(%rsp)
je 0x4027c3
movl 0x3c(%rsp), %eax
movl $0x5, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x4027c3
leaq 0xd8ed7(%rip), %rdi     # 0x4db68f
xorl %eax, %eax
callq 0x435910
jmp 0x4027c1
jmp 0x4027c3
movslq 0x3c(%rsp), %rsi
leaq 0x60(%rsp), %rdi
callq 0x413460
movq %rax, 0x10(%rsp)
jmp 0x4027d9
movq 0x10(%rsp), %rax
movsbl (%rax), %esi
leaq 0xd898d(%rip), %rdi     # 0x4db175
xorl %eax, %eax
callq 0x435910
jmp 0x4027f1
jmp 0x4027f3
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x40278d
leaq 0xdad83(%rip), %rdi     # 0x4dd58a
xorl %eax, %eax
callq 0x435910
jmp 0x402810
movl $0x0, 0xc64(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x412f20
movl 0xc64(%rsp), %eax
addq $0xc68, %rsp            # imm = 0xC68
retq
movq 0x50(%rsp), %rdi
callq 0x429470
nop