요약 - 보안 금고
Coddy Lua 여정의 객체 지향 프로그래밍 (Object Oriented Programming) 섹션에 포함된 레슨. 70개 중 48번째.
챌린지
쉬움클로저 기반 privacy를 사용하여 진정으로 secure한 저장 시스템을 만드는 Vault class를 만들어 봅시다! vault는 correct password를 제공해야만 Access할 수 있는 secret contents를 보관합니다.
코드를 두 파일에 나누어 구성합니다:
Vault.lua: contents와 password를 모두 constructor 내부의 local 변수에 저장하는 class를 만드세요. 외부 Access로부터 완전히 숨겨집니다. constructor:new(contents, password)는 이 값들을 local 변수에 저장한 다음, object instance에 직접 method를 정의해야 합니다::open(attemptedPassword): attempted password가 저장된 password와 matches하는지 확인합니다. correct하면 secret contents를 반환합니다. incorrect하면"Access Denied"를 반환합니다.
main.lua: Vault module을 Require하고 세 가지 inputs를 read합니다: 저장할 secret contents, 설정할 password, 그리고 password attempt입니다. contents와 password로 vault를 Create한 다음, attempted password로 vault를 open하고 result를 Print합니다. 또한vault.contents에 directly Access하여 얻은 값을 Print함으로써 contents가 진정으로 private임을 보여 주세요.
다음 세 가지 inputs를 받습니다:
- vault에 저장할 secret contents
- vault를 보호할 password
- vault를 open해 보기 위한 password attempt
출력은 다음 두 줄이어야 합니다:
Result: {resultFromOpen}
Direct access: {whatYouGetFromVaultContents}예를 들어 inputs가 Gold Coins, secret123, secret123인 경우 출력은 다음과 같아야 합니다:
Result: Gold Coins
Direct access: nilinputs가 Diamond, mypass, wrongpass인 경우 출력은 다음과 같아야 합니다:
Result: Access Denied
Direct access: nil여기서 핵심은 contents와 password가 모두 constructor 내부의 local 변수로만 존재한다는 점입니다. :open() method는 closure를 통해 이 변수들에 Access할 수 있지만, 그 외에는 누구도 Access할 수 없습니다. vault.contents, vault._contents, 또는 vault.password조차도 마찬가지입니다. 이것이 바로 진정한 캡슐화입니다!
직접 해보기
-- Vault 모듈을 불러옵니다
local Vault = require('Vault')
-- 입력을 읽습니다
local contents = io.read()
local password = io.read()
local attemptedPassword = io.read()
-- TODO: contents와 password로 vault를 생성합니다
-- TODO: attemptedPassword로 vault를 열어봅니다
-- TODO: 결과를 다음 형식으로 출력합니다: "Result: {resultFromOpen}"
-- TODO: vault.contents에 직접 접근하여 얻은 것을 출력합니다
-- Print in the format: "Direct access: {whatYouGetFromVaultContents}"
객체 지향 프로그래밍 (Object Oriented Programming)의 모든 레슨
직접 연습해 보세요: 온라인 Lua 컴파일러