เข้ารหัส password แค่นี้พอไหม?

$password = sha512hash($password.$salt);

ซึ่งตาม concept มันก็ใช้ได้แล้วเพราะมี salt มาช่วย แต่ทำไม cms หลายๆ เจ้าถึงได้มีโค้ดเข้ารหัสแบบซับซ้อนมาก อ่านแล้วก็งงว่ามันทำให้ปลอดภัยขึ้นจริงหรือ แล้วถ้าเราเขียนง่ายๆ แค่ข้างบนนี้มันดีพอหรือเปล่า

ใครเทพๆ ด้านการเข้ารหัสรบกวนด้วยครับ :)

Hiring! บริษัทที่น่าสนใจ

Carmen Software company cover
Carmen Software
Hotel Financial Solutions
Next Innovation (Thailand) Co., Ltd. company cover
Next Innovation (Thailand) Co., Ltd.
We are web design with consulting & engineering services driven the future stronger and flexibility.
KKP Dime company cover
KKP Dime
KKP Dime บริษัทในเครือเกียรตินาคินภัทร
Kiatnakin Phatra Financial Group company cover
Kiatnakin Phatra Financial Group
Financial Service
Fastwork Technologies company cover
Fastwork Technologies
Fastwork.co เว็บไซต์ที่รวบรวม ฟรีแลนซ์ มืออาชีพจากหลากหลายสายงานไว้ในที่เดียวกัน
Thoughtworks Thailand company cover
Thoughtworks Thailand
Thoughtworks เป็นบริษัทที่ปรึกษาด้านเทคโนโยลีระดับโลกที่คว้า Great Place to Work 3 ปีซ้อน
Iron Software company cover
Iron Software
Iron Software is an American company providing a suite of .NET libraries by engineer for engineers.
CLEVERSE company cover
CLEVERSE
Cleverse is a Venture Builder. Our team builds several tech companies.
Nipa Cloud company cover
Nipa Cloud
#1 OpenStack cloud provider in Thailand with our own data center and software platform.
Bangmod Enterprise company cover
Bangmod Enterprise
The leader in Cloud Server and Hosting in Thailand.
CIMB THAI Bank company cover
CIMB THAI Bank
MOVING FORWARD WITH YOU - CIMB is the leading ASEAN Bank
Bangkok Bank company cover
Bangkok Bank
Bangkok Bank is one of Southeast Asia's largest regional banks, a market leader in business banking
MuvMi (Urban Mobility Tech Co.,Ltd.) company cover
MuvMi (Urban Mobility Tech Co.,Ltd.)
Shape the future of urban mobility towards affordable, clean, and safe solutions
T.N. Digital Solution Co., Ltd. company cover
T.N. Digital Solution Co., Ltd.
TNDS has been involving in every first move of banking’s major digital transformation.
KBTG - KASIKORN Business-Technology Group company cover
KBTG - KASIKORN Business-Technology Group
KBTG - "The Technology Company for Digital Business Innovation"
Siam Commercial Bank Public Company Limited company cover
Siam Commercial Bank Public Company Limited
"Let's start a brighter career future together"
Icon Framework co.,Ltd. company cover
Icon Framework co.,Ltd.
Global Standard Platform for Real Estate แพลตฟอร์มสำหรับธุรกิจอสังหาริมทรัพย์ครบวงจร มาตรฐานระดับโลก
REFINITIV company cover
REFINITIV
The Financial and Risk business of Thomson Reuters is now Refinitiv
H LAB company cover
H LAB
Re-engineering healthcare systems through intelligent platforms and system design.
The Gang Technology Co., Ltd. company cover
The Gang Technology Co., Ltd.
We're a Digital Agency that helps our customers transform their business into digital with ease.
LTMH company cover
LTMH
LTMH มุ่งเน้นการพัฒนาผลิตภัณฑ์ที่สามารถช่วยพันธมิตรของเราให้บรรลุเป้าหมาย
Seven Peaks company cover
Seven Peaks
We Drive Digital Transformation
Wisesight (Thailand) Co., Ltd. company cover
Wisesight (Thailand) Co., Ltd.
The Best Choice For Handling Social Media · High Expertise in Social Data · Most Advanced and Secure
MOLOG Tech company cover
MOLOG Tech
We are Modern Logistic Platform, Specialize in WMS, OMS and TMS.
Data Wow Co.,Ltd company cover
Data Wow Co.,Ltd
We enable our clients to realize increased productivity by solving their most complex issues by Data
LINE Company Thailand company cover
LINE Company Thailand
LINE, the world's hottest mobile messaging platform, offers free text and voice messaging + Call
LINE MAN Wongnai company cover
LINE MAN Wongnai
Join our journey to becoming No.1 food platform in Thailand

ผมก็ไม่รู้ว่ามันจะแข็งแรงพอหรือปล่าว ส่วนที่ซับซ้อนที่สุดน่าจะเป็นวิธีคิดเพื่อให้ได้มาของ $salt (โดย $salt ไม่ควรเป็นค่าคงที่)

ถ้า salt ไม่ควรคงที่ แล้วเวลาเรา login และตรวจสอบ password ค่า salt เปลี่ยนไปค่า hash ที่ได้ก็ไม่เหมือนเดิม แล้วจะเปรียบเทียบ password ได้ยังไงล่ะครับ?

หรือหมายถึงว่า salt ของแต่ละ user ไม่ควรเหมือนกัน แต่ต้องเป็นค่าของใครของมัน?

ถ้าเป็นอย่างนั้นเราใช้ username/id เป็นค่า salt ได้หรือไม่? แต่ก็มีปัญหาต่ออีกว่ามันก็เป็นข้อมูลที่เปิดเผยอยู่ดี แล้วเราจะเก็บค่า salt ไว้ที่ไหนล่ะ

ตรงนี้แหละที่ผมสงสัยมากครับ

Ford AntiTrust Sat, 08/03/2014 - 20:18

ค่า salt ไม่ควรคงที่ถูกต้องแล้วครับ

logic จะประมาณด้านล่าง ตอน save ลง DB ให้เอา salt ผูกลงไปด้วยครับ ไว้ใช้ในการเปรียบเทียบตอน authen ครับ

ผมขอโค้ดสดๆ ลองดูนะครับ

$salt = md5(time());
$passhash = md5('password'.$salt);

$storetodb = $passhash.':'.salt;

===============

$passlist = explode(':', $hashfromdb);
$passhashindb = $passlist[0];
$salt = $passlist[1];

$passhashinput = md5($inputpassword.$salt);

if(passhashindb == $passhashinput) {
// valid
} else {
// invalid
}

โอเคเลยคุณฟอร์ด แบบนี้ผมเข้าใจเลย รูปแบบคล้ายๆ ของ Joomla! เก็บ salt ไว้กับ password แบบนี้เลย คำถามที่ยังคาใจอยู่ก็คือ ทำไมโค้ดของ cms หลายตัว (แม้กระทั่ง Joomla! 3 เองก็ตาม) ไม่ได้ดูเรียบง่ายแบบนี้ มันดูซับซ้อนเกินจะแกะไหว (ผมไม่มีพื้นฐานการเข้ารหัสเท่าไหร่ด้วย กำลังไล่อ่าน paper อยู่)

คำถามสุดท้าย...หากโค้ดของเราเรียบง่ายเหมือนตัวอย่างที่ผมให้ไว้ข้างบนหรือแบบของคุณฟอร์ดก็ตาม มันดีพอสำหรับการเข้ารหัส password หรือเปล่าครับ? ทั้งที่มันก็ถูกต้องตาม concept การเข้ารหัสที่ตำราต่างๆ บอกไว้ว่าเป็นที่ยอมรับกันโดยทั่วไป (ผมถามโดยเทียบกับความซับซ้อนของ cms ต่างๆ ที่ยังเป็นปริศนาสำหรับผมอยู่นะ)

โค้ดส่วนมากใน joomla authen ที่เห็นเยอะๆส่วนมากก็เป็นการเช็คค่าต่างๆนะครับ ลองดูตรง comment ของ code ผมว่าอธิบายได้ครบเลย โค้ดตรง authen ส่วน password จริงๆมีแค่นี้

public function onUserAuthenticate($credentials, $options, &$response)
{
$response->type = 'Joomla';
// Joomla does not like blank passwords
if (empty($credentials['password']))
{
$response->status = JAuthentication::STATUS_FAILURE;
$response->error_message = JText::('JGLOBAL_AUTH_EMPTY_PASS_NOT_ALLOWED');
return false;
}
// Get a database object
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select('id, password')
->from('#__users')
->where('username=' . $db->quote($credentials['username']));
$db->setQuery($query);
$result = $db->loadObject();
if ($result)
{
$match = JUserHelper::verifyPassword($credentials['password'], $result->password, $result->id);
if ($match === true)
{
// Bring this in line with the rest of the system
$user = JUser::getInstance($result->id);
$response->email = $user->email;
$response->fullname = $user->name;
if (JFactory::getApplication()->isAdmin())
{
$response->language = $user->getParam('admin_language');
}
else
{
$response->language = $user->getParam('language');
}
$response->status = JAuthentication::STATUS_SUCCESS;
$response->error_message = '';
}
else
{
// Invalid password
$response->status = JAuthentication::STATUS_FAILURE;
$response->error_message = JText::
('JGLOBAL_AUTH_INVALID_PASS');
}
}
else
{
// Invalid user
$response->status = JAuthentication::STATUS_FAILURE;
$response->error_message = JText::_('JGLOBAL_AUTH_NO_USER');
}

เข้าใจว่าหลายๆ cms ใช้การสร้าง salt ที่ไม่มีแบบแผนตายตัว หรือไม่ให้ซ้ำกันในระดับ microsecond ซึ่งก็แล้วแต่ว่าแต่ละเจ้าจะเป็นแบบไหน แต่สุดท้าย logic ในภาพรวมก็ไม่แตกต่างกันครับ ด้านบนนั้นเป็นแบบ joomla และ drupal ใช้ เพราะผมก็แกะมาจากทั้งสองตัวนี้มาเป็น case study ครับ

ส่วนที่โครงสร้างโปรแกรมมันซํบซ้อน อาจจะเพราะป้องกันการแกะหรือป้องกัน injection ในเชิงโครงสร้างเวลาทำพวก module หรือ plugin น่ะครับ (ผมเดาเอานะ เพราะหลายตัวก็ลากไปโน้นนี่ extend เยอะแยะไปหมด)

แวะมาบอกว่าในคอมเม้น ถ้าเว้นวรรคข้างหน้า 4 วรรค (เหมือน stack overflow) โค๊ดจะฟอร์แมทถูกครับ ถึงจะไม่มี syntax highlight ก็เถอะ