c 에서 비밀번호를 입력받을때...

익명 사용자의 이미지


일반적인 유틸 사용시 password를 입력받을때, "*"표기로 화면에 뿌려지는데

이걸 c 에서 구현하려면 어떻게 해야 하죠?

bash에서는 그런 기능이 있긴 한데, 그래서 c에서도 어떤 함수나 라이브러리
가 있을거 같은데...

답변 부탁드립니다.

익명 사용자의 이미지

흠...
getpass 라는 것이 있긴 했는데..

97년도에 마지막으로 써보고 못만져 봐서..
지금도 쓸수 있는 지 더 발전 되었는 지는 모르겠지만..

man 페이지가 존재하는 것을 보니 , 있는 것 같네요..
가능하시다면, passwd 프로그램의 소스를 보시지요..

smbpasswd 는 아직 이넘을 쓰는 것 같은데요..

익명 사용자의 이미지

c 에서 비밀번호를 입력받는 방법은 특별한 함수가 있는것이 아니라 대부분이
해당하는 터미날의 속성을 바꾸어서 처리 합니다. 예를 들어서 터미날 속성중
echo 부분을 제외시키면 입력한 문자가 화면에 보이지 않게 됩니다.

터미날 속성은 termios라는 구조체를 보시면 자세하게 나와 있습니다.

터미날 속성을 변경하는 방법은 먼저 tcgetattr 함수를 사용하여 termios 구조
체에 기존의 속성값을 저장한후 tcsetattr를 사용하여 속성을 변경하면 됩니
다. 그리고 모든 처리가 끝나면 이전에 저장한 termios 구조체로 tcsetattr를
호출하면 원래의 속성값으로 돌아옵니다.

예를 들어 현재 터미날에서 echo 속성를 없애는 방법은

#include
#include

int main()
{
struct termios init_term;
struct termios new_term;

tcgetattr(fileno(stdin), &init_term);

new_term = init_term;
new_term.c_lflag &= ~ECHO;

tcsetattr(fileno(stdin), TCSAFLUSH, &new_term);

....

tcsetattr(fileno(stdin), TCSANOW, &init_term);
}

익명 사용자의 이미지

흠..구지 함수까지 필요가 있나요
그럴필요 없이 윗분이 말했듯이 터미널 속성에서 echo를 제거해 주면
됩니다.그리고 getchar()같은 함수로 한문자씩 받아서 '\n'이 나오는것을
체크 하면서 *문자를 보내주면 되겠죠.

char password[BUF_SIZE]={0,};
int temp,index=0;
struct termios a,b;

tcgetattr(fileno(stdin),&a);
b=a;
b.c_lflag=~ECHO;
tcsetattr(fileno(stdin),TCSANOW,&b);

printf("Password");
while(buffer[index++]=(temp=getchar())!='\n')
putchar('*');

익명 사용자의 이미지

님들.... ^^*

너무도 친절한 답변에 진심으로 감사드립니다.

정말 많은 도움이 됐습니다.

감사합니다.

sun1226의 이미지

getchar()같은 함수로 한문자씩 받아서 '\n'이 나오는것을
체크 하면서 *문자를 보내주면 되겠죠

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.