{"id":135410,"date":"2024-09-06T12:31:01","date_gmt":"2024-09-06T07:01:01","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?page_id=135410"},"modified":"2024-09-06T12:31:02","modified_gmt":"2024-09-06T07:01:02","slug":"password-hashing-with-passlib","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/password-hashing-with-passlib\/","title":{"rendered":"Password Hashing with Passlib"},"content":{"rendered":"\n<p>Storing plain-text passwords in a database is a security risk. To protect user credentials, it&#8217;s essential to use strong password hashing algorithms. Passlib is a popular Python library that provides various password hashing algorithms and utilities. In this guide, we&#8217;ll demonstrate how to use Passlib to hash passwords in a FastAPI application.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Installing Passlib<\/strong><\/h2>\n\n\n\n<p>Install Passlib using pip:<\/p>\n\n\n\n<p>Bash<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install passlib\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Hashing Passwords<\/strong><\/h2>\n\n\n\n<p>Use the <code>hash<\/code> function from Passlib to hash a password:<\/p>\n\n\n\n<p>Python<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from passlib.context import CryptContext\n\npwd_context = CryptContext(schemes=&#91;\"bcrypt\"], deprecated=\"auto\")\n\ndef hash_password(password: str):\n    return pwd_context.hash(password) &nbsp; \n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Verifying Passwords<\/strong><\/h2>\n\n\n\n<p>Use the <code>verify<\/code> function to verify a password against a hashed password:<\/p>\n\n\n\n<p>Python<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def verify_password(plain_password, hashed_password):\n    return pwd_context.verify(plain_password, hashed_password)\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Integrating with FastAPI<\/strong><\/h2>\n\n\n\n<p>Modify your user registration endpoint to hash the password before storing it in the database:<\/p>\n\n\n\n<p>Python<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from app.utils import hash_password\n\n@router.post(\"\/register\", response_model=UserSchema)\nasync def register_user(user: UserCreate, db: Session = Depends(get_db)):\n    # ...\n    hashed_password = hash_password(user.password)\n    db_user = User(email=user.email, password=hashed_password)\n    # ...\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Testing Password Hashing<\/strong><\/h2>\n\n\n\n<p>Write tests to ensure that passwords are hashed correctly and cannot be easily reversed:<\/p>\n\n\n\n<p>Python<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def test_hash_password(client):\n    password = \"password123\"\n    hashed_password = hash_password(password)\n    assert hashed_password != password\n    assert pwd_context.verify(password, hashed_password)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Storing plain-text passwords in a database is a security risk. To protect user credentials, it&#8217;s essential to use strong password hashing algorithms. Passlib is a popular Python library that provides various password hashing algorithms and utilities. In this guide, we&#8217;ll demonstrate how to use Passlib to hash passwords in a FastAPI application. Installing Passlib Install&#8230;<\/p>\n","protected":false},"author":16,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-135410","page","type-page","status-publish","hentry"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Password Hashing with Passlib - Tutorial<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/password-hashing-with-passlib\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Password Hashing with Passlib - Tutorial\" \/>\n<meta property=\"og:description\" content=\"Storing plain-text passwords in a database is a security risk. To protect user credentials, it&#8217;s essential to use strong password hashing algorithms. Passlib is a popular Python library that provides various password hashing algorithms and utilities. In this guide, we&#8217;ll demonstrate how to use Passlib to hash passwords in a FastAPI application. Installing Passlib Install...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/password-hashing-with-passlib\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-09-06T07:01:02+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/password-hashing-with-passlib\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/password-hashing-with-passlib\/\",\"name\":\"Password Hashing with Passlib - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2024-09-06T07:01:01+00:00\",\"dateModified\":\"2024-09-06T07:01:02+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/password-hashing-with-passlib\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/password-hashing-with-passlib\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/password-hashing-with-passlib\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Password Hashing with Passlib\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Password Hashing with Passlib - Tutorial","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/password-hashing-with-passlib\/","og_locale":"en_US","og_type":"article","og_title":"Password Hashing with Passlib - Tutorial","og_description":"Storing plain-text passwords in a database is a security risk. To protect user credentials, it&#8217;s essential to use strong password hashing algorithms. Passlib is a popular Python library that provides various password hashing algorithms and utilities. In this guide, we&#8217;ll demonstrate how to use Passlib to hash passwords in a FastAPI application. Installing Passlib Install...","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/password-hashing-with-passlib\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-09-06T07:01:02+00:00","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/password-hashing-with-passlib\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/password-hashing-with-passlib\/","name":"Password Hashing with Passlib - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2024-09-06T07:01:01+00:00","dateModified":"2024-09-06T07:01:02+00:00","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/password-hashing-with-passlib\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/password-hashing-with-passlib\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/password-hashing-with-passlib\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Password Hashing with Passlib"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/135410","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=135410"}],"version-history":[{"count":2,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/135410\/revisions"}],"predecessor-version":[{"id":135428,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/135410\/revisions\/135428"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=135410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=135410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=135410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}