(MATLAB) SHA256 암호화 기반 Login 기능 구현

필요기능

계정 생성

  • ID, Password
  • Password 암호화
  • 계정 정보 저장 (.mat 파일)

로그인

  • ID 매칭
  • Password 매칭

구현

암호화 알고리즘

  • SHA256
    • 256비트, 64자리 문자열
    • 미국 국립표준기술연구소 (NIST)에 의해 공표된 표준 해시 알고리즘 SHA-2 계열 중 하나
    • 블록체인에서 가장 많이 채택하여 사용
    • 비교적 안전하다고 평가
      SHA256
    • C/C++, Java, Python 등의 암호화 알고리즘 구현 코드는 한국인터넷진흥원 (KISA) 홈페이지 등을 통해 공개
      KISA 암호이용활성화 - 자료실 - 암호알고리즘 소스코드

Matlab 구현 중점

소스코드

  • 암호화 함수 (stringToHashedHex.m 파일)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    function hashedHex = stringToHashedHex(string)

    salt = 'salt';
    sha256hasher = System.Security.Cryptography.SHA256Managed;
    saltedString = strcat(string, salt);
    sha256 = uint8(sha256hasher.ComputeHash(uint8(saltedString)));
    hashedHex = sha256;

    end
  • 암호화 함수 pcode화

    1
    >> pcode('stringToHashedHex.m')
  • 계정 생성 함수 (generateAccount.m)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    function generateAccount(id, password)

    MIN_PASSWORD_LENGTH = 5;
    MAX_PASSWORD_LENGTH = 20;

    if length(password) < MIN_PASSWORD_LENGTH
    fprintf('Password should be longer than %d words\n', MIN_PASSWORD_LENGTH);
    return;
    end

    if length(password) > MAX_PASSWORD_LENGTH
    fprintf('Password should be shorter than %d words\n', MAX_PASSWORD_LENGTH);
    return;
    end

    Accounts = struct;
    hashedPassword = stringToHashedHex(password);

    if isfile('accounts.mat')
    loadedVariable = load('accounts.mat');
    Accounts = loadedVariable.Accounts;
    end

    if isfield(Accounts, id)
    disp('Exist ID');
    return;
    end

    Accounts.(id).Password = hashedPassword;
    save('accounts.mat', 'Accounts');

    end
    • stringToHashedHex 함수를 통해 입력된 password를 암호화
    • account.mat 파일을 생성하여 저장
    • 저장되는 변수 형식은 struct (변수명 : Accounts)
    • 예를들어, 아이디 : “user1”, 비밀번호 : “123456789” 인 경우 Accounts.user1.Password = “123456789”
  • 로그인 함수 (loginAccount.m)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    function loginAccount(id, password)

    if isempty(id); disp('Enter the ID'); return; end
    if isempty(password); disp('Enter the Password'); return; end

    Accounts = struct;
    hashedPassword = stringToHashedHex(password);

    if isfile('accounts.mat')
    loadedVariable = load('accounts.mat');
    Accounts = loadedVariable.Accounts;
    end

    if ~isfield(Accounts, id)
    disp('Invalid ID');
    return;
    end

    if ~isfield(Accounts.(id), 'Password')
    disp('Invalid Account. Please contact the Administrator!');
    return;
    end

    if isequal(hashedPassword, Accounts.(id).Password)
    disp('Login Succeed');
    else
    disp('Invalid Password!')
    end

    end
    • account.mat 파일을 불러온 후, 입력된 id와 일치하는 필드 확인
    • stringToHashedHex 함수를 통해 입력된 비밀번호 암호화
    • 암호화 된 비밀번호와 Account 변수에 저장되어 있는 암호화 코드 비교

구현 이미지

  • 계정 생성 (아이디 : “user1”, 비밀번호 : “123456789”)
  • account.mat 파일 생성 확인
  • account.mat 내 Account 변수 구조 및 내용 확인

  • 로그인 기능 확인

추가 필요기능 (향후 구현)

  • 입력된 아이디, 비밀번호의 데이터 형식 확인 (string만 가능하도록)
  • 비밀번호 안정성을 위한 필터 기능 (특수문자, 대문자 등)

Reference