Menu
Coddy logo textTech

요약 - 보안 락커

Coddy Rust 여정의 Object Oriented Programming 섹션에 포함된 레슨 — 61개 중 13번째.

challenge icon

챌린지

쉬움

지금까지 배운 캡슐화의 모든 개념을 활용하여 보안 사물함 시스템을 구축해 봅시다! 여러분의 사물함은 비밀번호 뒤에 내용을 보호하며, 비밀번호를 아는 사람만이 내부를 볼 수 있습니다.

코드를 정리하기 위해 두 개의 파일을 생성합니다:

  • locker.rs: 두 개의 비공개(private) 필드인 code (u32)와 contents (String)를 가진 Locker 구조체를 정의합니다. 구조체는 공개(public)되어야 하지만, 사물함의 보안을 위해 두 필드는 반드시 비공개로 유지되어야 합니다. 다음을 구현하세요:
    • 초기 비밀번호와 내용을 인자로 받는 new 생성자
    • 사물함의 비밀번호를 변경할 수 있는 set_code 메서드
    • 시도할 비밀번호를 인자로 받는 get_contents 메서드 — 비밀번호가 사물함의 비밀번호와 일치하면 내용을 반환하고, 그렇지 않으면 "Access denied"를 반환합니다.
  • main.rs: 작성한 사물함 모듈을 가져와서 사물함을 생성하고, 올바른 비밀번호와 잘못된 비밀번호로 내용 접근을 시도하여 보안 기능을 시연합니다.

get_contents 메서드는 String을 반환해야 합니다. 비밀번호가 맞으면 실제 내용을, 틀리면 거부 메시지를 반환합니다. 이것은 비공개 데이터를 공개하기 전에 액세스 권한을 검증하는 조건부 게터(getter)입니다.

출력은 반드시 다음 형식을 따라야 합니다:

Attempt with {code}: {result}
Code changed
Attempt with {code}: {result}

예를 들어, 비밀번호 1234와 내용 "Gold coins"로 사물함을 생성한 후, 비밀번호 0000으로 시도하고, 비밀번호를 5678로 변경한 뒤, 다시 5678로 시도한다면 출력은 다음과 같아야 합니다:

Attempt with 0000: Access denied
Code changed
Attempt with 5678: Gold coins

입력으로 초기 비밀번호, 내용, 첫 번째 시도 비밀번호, 새로 설정할 비밀번호, 그리고 두 번째 시도 비밀번호까지 총 다섯 개의 값을 받게 됩니다.

직접 해보기

mod locker;

use locker::Locker;

fn main() {
    // 입력 읽기
    let mut input = String::new();
    std::io::stdin().read_line(&mut input).expect("Failed to read line");
    let initial_code: u32 = input.trim().parse().expect("Invalid number");

    let mut input = String::new();
    std::io::stdin().read_line(&mut input).expect("Failed to read line");
    let contents = input.trim().to_string();

    let mut input = String::new();
    std::io::stdin().read_line(&mut input).expect("Failed to read line");
    let first_attempt: u32 = input.trim().parse().expect("Invalid number");

    let mut input = String::new();
    std::io::stdin().read_line(&mut input).expect("Failed to read line");
    let new_code: u32 = input.trim().parse().expect("Invalid number");

    let mut input = String::new();
    std::io::stdin().read_line(&mut input).expect("Failed to read line");
    let second_attempt: u32 = input.trim().parse().expect("Invalid number");

    // TODO: initial_code와 contents를 사용하여 새로운 Locker 생성

    // TODO: first_attempt 코드로 내용물 가져오기 시도
    // 출력: "Attempt with {code}: {result}"

    // TODO: 사물함 코드를 new_code로 변경
    // 출력: "Code changed"

    // TODO: second_attempt 코드로 내용물 가져오기 시도
    // 출력: "Attempt with {code}: {result}"
}

Object Oriented Programming의 모든 레슨