Go tutorial Part 11 (Web app Part 9)

Tensor Programming · Beginner ·🛠️ AI Tools & Apps ·9y ago

Key Takeaways

This video tutorial demonstrates how to create a web application in Go, focusing on user authentication, password hashing, and unique user ID generation using tools like bcrypt, crypto/rand, and UUID.

Full Transcript

hi guys welcome to go tutorial part 11 my name is tensor from the tensor programming blog and today we're just going to do a rather quick tutorial and we're going to implement a method to hash our password using bcrypt and we are also going to create our unique user ID for our table first before we start coding let's go over what exactly these two things are so our universal unique identifier is basically just going to look like this so as you can see there's eight characters here then 4 then 4 then 4 and then 2012 and these are all generated by random numbers then for bcrypt bcrypt is basically just a hashing function that was created back in 99 and it's pretty much the default hashing algorithm used on many of the Linux distributions and it's pretty it's a very strong way of actually encrypting a password so currently if we look at our data inside of our SQL file you'll see here that our password is in plain text this is not something that we want generally when you create a web app that has user validation you want the users to be able to sign in and you want to be able to store their passwords but you never want to be able to store their passwords in plain text because first of all would be insecure because anybody who had had admin access would be able to just break into your SQL database and you know access anybody else's account but also because it would be very easy for somebody to hack and so if somebody tries to hack this particular database as it is currently all they have to do is guess what the white password is whereas if we encrypt it with bcrypt then they have to guess both the password and the salt that we use to hash the actual password ok so let's get started so let's actually integrate the bcrypt right now so go has its own bcrypt library it's not a standard library but it is pretty well used library so let's import it the library that we're going to need is going down orb backslash expec slash crypto backslash bcrypt and this is going to allow us to encrypt our password so we're going to create a function and let's call it encrypt pass and we're gonna pass in our password which would be a type string of course and then we are going to pass back a string sorry encrypt password is going to be rather easy so we're going to bring in our password and then we are going to convert it to a byte or rather a slice of bytes so we will say pass equals slice of bytes password hashing so there is a function in our bcrypt that allows us to simply just hash our password so we will say hash PW comma underscore and equals and let's call our library bcrypt and then the function is called generate from password and in here we enter in our password and then we enter in the cost which is basically the amount of computational power that we want to encrypt this particular password with so in this case we're just going to use the default cost which is actually just 10 so I can just put in 10 and it will work but I'd rather just actually bring in our default cost so bcrypt default cost so now we have our hash password and we just need to return it as a string because it's a slice of ice so now we will actually encrypt the password and it will return it as a string so we have this user exists' function up here and this what this does is it queries the database for the username and password where the username and password equal with such-and-such so we need to amend it and this will allow us to validate the user based on the hashed password so the way we amend it is we come down here between where we scan our password and where we actually check if the password equals the password that we're bringing in from the user instance and we say password equal be crypts and Paran hash password and then we're gonna pass in a slice of bytes for our password which is the one that's being brought out of the SQLite database and then we are going to pass in our Utah password which is the password that's being passed through the form for validation so what this will do is it will compare the two you know compare our password say it's just password or our one two three four five six with the hashed version of one two three four five six and it will either send back nil if they are actually equal or it will send back an error if they aren't so we can come here instead of saying PS equals Utah password we can say P W equals nil so if this comes back as no that means that they're both equal and so we want to return true which means our user exists in the database we also want to amend this here because the way that we're searching for our username and password is we are actually searching for a username with this username so the instance username and the password with that user password but unfortunately this password is not going to be in our database anymore so if we just remove this part entirely and just search for the username things should work much better so now we can go into our main deco file and we can come down here to our signup function and where we are actually bringing in the password we can just call our function that encrypt it straight from the forum value and what this will do is it will encrypt the password and then when we put it into our database it will be encrypted so at no point in our API where we have the rights password which is nice though if you're going to do things like this if you're working on like say a commercial version I would probably use it in between variable and I call the function directly normally you'd have some kind of error that you'd be passing back from the function as well so generally you wouldn't want to do it like this but because of the way that we're building this user function it shouldn't be a problem okay so now we need to create our unique user ID so if you remember the way the unique user ID works is it is a bunch of random numbers where we have eight numbers in the front then four then 4 then four and then 12 so the way our function is going to work is we're going to input nothing and then we're going to output an ID of string so we want to have a global variable called ID and we're going to say okay be make and we're going to make a array of bytes and this is going to be of 16 of size 16 and the reason why it's going to be of size 16 despite the fact that our actual user ID is of size 32 is because a byte actually is two characters so this actually will work to our advantage so we're only using a size 16 but it will actually look like it's 32 so now we need to generate our random numbers and we're going to use a library that exists in golang so let's actually call ran dot read then we're gonna set and be in there and our library is called crypto r and so here it is crypto brand and this is allowing us to create or to put random numbers into this byte but they're not just random numbers random characters then we need to check our error so we're going to say if error it's not equal now return and this will just quit the function if we have an error otherwise we need to say ID equals fmt dot s print F and this will allow us to format our string for a return and the way we want to format it is we want to put dashes between a certain amount of characters so if you remember what it looked like in the Wikipedia article as you can see there are six characters here and then a dash then 4 then 4 then 4 and then another set of characters so all we really need to do is make it look like this so we're going to say okay so our fur X is going to be be zero to four our second X will be four and six because remember two so this is only two parts of the array but it will be four characters long and as you can see for our next two we also do two characters long so six to eight and then eight to ten and then we do the rest which is ten to sixteen so we just can call ten to the end and this will format it the way that we want so then all we have to do at the end is just hit return and this will return our ID because we have already defined it up here as our return value so now we need to actually edit our save data function here so that it will actually save the unique user ID so we want to set in our unique user ID as a text field and then we want to set it in here as well which means we also need to add another question mark in here and then we want to call it in our exec function as udu ID so this will work but right now of course we're not actually setting it in through main so we want to actually call UUID and then we're going to call our function UUID so this will generate a unique user ID and set it into this field automatically so we need to delete our database because as it is currently we only have five columns and we just made it so that we have six columns so we could either delete the database or delete the table so let's just delete the table now if we open up the database again it has no table in it so let's run our file real quick and see what happens so here we are at the signup page and let's sign up okay so we'll put in tensor with a capital T and then we'll put in John Doe and we'll put in test at example.com and our password will be one two three four five six we'll hit submit now let's take a look at our SQL write database so inside of our database we have our unique user ID as you can see it's formatted in the way we wanted which is nice we also have our password and as you can see this is not one two three four five six rather this is being completely hashed and in fact to demonstrate my point first let's login but so it's actually checking the one two three four five six with the hashed password if we sign up again let's say tensor to first name John - Doh - and let's give her an email of just test a test comm and say you know let's put in the same password one two three four five six and hit submit if we look at this in the database as you can see here the first team characters are the same so it goes a dollar sign then - way then a dollar sign then 10 then a dollar sign and then from there everything else is completely different but even though these are the same passwords in reality they will actually come back as correct if we try to match them so if I try to sign in as 10 sir - and I just type in 1 2 3 4 5 6 it will still work even though there are two separate hashes so this is kind of cool and as you can see our unique ID has also been updated and it's completely different alright guys so I hope you enjoyed this tutorial in our next tutorial we'll actually start to look at how we can add more user validation to this application and we will look at why we actually created this unique user ID if you enjoyed this video please feel free to like and subscribe if you have any questions of course feel free to comment and if you dislike the video then feel free to download it I hope you guys have a happy new year

Original Description

In this quick tutorial we create our Uuid and hash our password. Also check out our written Golang tutorial on our blog: http://tensor-programming.com/welcome-to-go/ Check out the Code on Github: https://github.com/tensor-programming/go-tutorial-11 Check out our Twitter: https://twitter.com/TensorProgram Check out our Facebook: https://www.facebook.com/Tensor-Programming-1197847143611799/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Tensor Programming · Tensor Programming · 19 of 60

1 NodeJs, Text editors and IDEs
NodeJs, Text editors and IDEs
Tensor Programming
2 Vanilla JS todo App
Vanilla JS todo App
Tensor Programming
3 Elm Tutorial part 1
Elm Tutorial part 1
Tensor Programming
4 Elm Lang Tutorial, Part 2
Elm Lang Tutorial, Part 2
Tensor Programming
5 Elm Tutorial Part 3
Elm Tutorial Part 3
Tensor Programming
6 Elm Tutorial Part 4 -- Analog Clock App
Elm Tutorial Part 4 -- Analog Clock App
Tensor Programming
7 Elm Tutorial part 5 -- Snake Game
Elm Tutorial part 5 -- Snake Game
Tensor Programming
8 Elm Tutorial part 6 -- Calculator
Elm Tutorial part 6 -- Calculator
Tensor Programming
9 Go Tutorial part 1 -- Hello World and Static File Server
Go Tutorial part 1 -- Hello World and Static File Server
Tensor Programming
10 Go Tutorial part 2 -- Web Crawler
Go Tutorial part 2 -- Web Crawler
Tensor Programming
11 Go Tutorial Part 3 (Web App part 1)
Go Tutorial Part 3 (Web App part 1)
Tensor Programming
12 Go tutorial Part 4 (Web tutorial part 2) - Using templates
Go tutorial Part 4 (Web tutorial part 2) - Using templates
Tensor Programming
13 Go tutorial part 5 (web app part 3)
Go tutorial part 5 (web app part 3)
Tensor Programming
14 Go tutorial part 6 (webapp part 4)
Go tutorial part 6 (webapp part 4)
Tensor Programming
15 Go tutorial part 7 (web app part 5)
Go tutorial part 7 (web app part 5)
Tensor Programming
16 Go tutorial part 8 (Web app part 6)
Go tutorial part 8 (Web app part 6)
Tensor Programming
17 Go tutorial Part 9 (web tutorial part 7)
Go tutorial Part 9 (web tutorial part 7)
Tensor Programming
18 Go tutorial Part 10 (web app part 8)
Go tutorial Part 10 (web app part 8)
Tensor Programming
Go tutorial Part 11 (Web app Part 9)
Go tutorial Part 11 (Web app Part 9)
Tensor Programming
20 Go Tutorial Part 12 (Web app Part 10)
Go Tutorial Part 12 (Web app Part 10)
Tensor Programming
21 Go Tutorial Part 13 (Web app Part 11)
Go Tutorial Part 13 (Web app Part 11)
Tensor Programming
22 Looking at Elm 0.18
Looking at Elm 0.18
Tensor Programming
23 Go tutorial Part 14 (Web tutorial part 12)
Go tutorial Part 14 (Web tutorial part 12)
Tensor Programming
24 Go tutorial Part 15 (Web tutorial part 13)
Go tutorial Part 15 (Web tutorial part 13)
Tensor Programming
25 Go tutorial part 16 (web app part 14)
Go tutorial part 16 (web app part 14)
Tensor Programming
26 Elm Tutorial Part 7 (SPA part 1)
Elm Tutorial Part 7 (SPA part 1)
Tensor Programming
27 Elm Tutorial Part 8 (SPA Part 2)
Elm Tutorial Part 8 (SPA Part 2)
Tensor Programming
28 Electron Elm Tutorial
Electron Elm Tutorial
Tensor Programming
29 Go tutorial part 17 (web app part 15)
Go tutorial part 17 (web app part 15)
Tensor Programming
30 Up and Coming Programming Languages and Technologies for 2017
Up and Coming Programming Languages and Technologies for 2017
Tensor Programming
31 elixir tutorial part 1
elixir tutorial part 1
Tensor Programming
32 elixir tutorial part 2
elixir tutorial part 2
Tensor Programming
33 Elixir tutorial Part 3 (GenServer and Supervisor)
Elixir tutorial Part 3 (GenServer and Supervisor)
Tensor Programming
34 Elixir Tutorial Part 4 (GenStage)
Elixir Tutorial Part 4 (GenStage)
Tensor Programming
35 Elixir Tutorial Part 5 (Plug and Cowboy)
Elixir Tutorial Part 5 (Plug and Cowboy)
Tensor Programming
36 Phoenix Framework Tutorial Part 1 (elixir part 6)
Phoenix Framework Tutorial Part 1 (elixir part 6)
Tensor Programming
37 Phoenix Framework Tutorial Part 2  (elixir part 7)
Phoenix Framework Tutorial Part 2 (elixir part 7)
Tensor Programming
38 Phoenix Framework Tutorial Part 3 (elixir part 8)
Phoenix Framework Tutorial Part 3 (elixir part 8)
Tensor Programming
39 A Intro to Clojure and Clojure Syntax
A Intro to Clojure and Clojure Syntax
Tensor Programming
40 An Update about the channel
An Update about the channel
Tensor Programming
41 Intro to Rustlang (Setup and Primitives)
Intro to Rustlang (Setup and Primitives)
Tensor Programming
42 Intro to Rustlang (Strings, Tuples, Arrays, Slices and Pretty Printing)
Intro to Rustlang (Strings, Tuples, Arrays, Slices and Pretty Printing)
Tensor Programming
43 Intro to Rustlang (Ownership and Borrowing)
Intro to Rustlang (Ownership and Borrowing)
Tensor Programming
44 Intro to Rustlang (Structs, Methods, Functions, Related Functions and the Display/Debug Traits)
Intro to Rustlang (Structs, Methods, Functions, Related Functions and the Display/Debug Traits)
Tensor Programming
45 Intro to Rustlang (Control Flow, Conditionals and Pattern Matching)
Intro to Rustlang (Control Flow, Conditionals and Pattern Matching)
Tensor Programming
46 Intro to RustLang (Enums and Options)
Intro to RustLang (Enums and Options)
Tensor Programming
47 Intro to Rustlang (Vectors, HashMaps, Casting, If-Let, While-Let, and the Result Enum)
Intro to Rustlang (Vectors, HashMaps, Casting, If-Let, While-Let, and the Result Enum)
Tensor Programming
48 Rustlang Project: Snake Game
Rustlang Project: Snake Game
Tensor Programming
49 Intro to Rustlang (Traits and Generic Types)
Intro to Rustlang (Traits and Generic Types)
Tensor Programming
50 Intro to Rust-lang (Closures, the Box Pointer and Iterators)
Intro to Rust-lang (Closures, the Box Pointer and Iterators)
Tensor Programming
51 Intro to Rust-lang (Modules and Lifetimes)
Intro to Rust-lang (Modules and Lifetimes)
Tensor Programming
52 Intro to Rust-lang (Macros and Metaprogramming)
Intro to Rust-lang (Macros and Metaprogramming)
Tensor Programming
53 Intro to Rust-lang (Error Handling)
Intro to Rust-lang (Error Handling)
Tensor Programming
54 Intro to Rust-lang (Concurrency, Threads, Channels, Mutex and Arc)
Intro to Rust-lang (Concurrency, Threads, Channels, Mutex and Arc)
Tensor Programming
55 Intro to Rust-lang (Tests, Attributes, Configuration and Conditional compilation)
Intro to Rust-lang (Tests, Attributes, Configuration and Conditional compilation)
Tensor Programming
56 Rustlang Project: Port Sniffer CLI
Rustlang Project: Port Sniffer CLI
Tensor Programming
57 Rustlang Project: Chat Application
Rustlang Project: Chat Application
Tensor Programming
58 Rustlang Project: CLI Toy Blockchain
Rustlang Project: CLI Toy Blockchain
Tensor Programming
59 Intro to Rust-lang (Setting up a Development Environment)
Intro to Rust-lang (Setting up a Development Environment)
Tensor Programming
60 Intro to Rust-lang (Building a Web API with Iron)
Intro to Rust-lang (Building a Web API with Iron)
Tensor Programming

This video tutorial teaches how to create a web application in Go, focusing on user authentication, password hashing, and unique user ID generation. It covers the use of bcrypt, crypto/rand, and UUID to secure user data.

Key Takeaways
  1. Import the bcrypt library
  2. Create a function to hash passwords using bcrypt
  3. Use crypto/rand to generate unique user ID
  4. Encrypt password with Paran hash before comparing with hashed version in database
  5. Remove password from database search to improve functionality
  6. Call UUID function to generate unique user ID
  7. Update user ID and password in database after signup
💡 Using bcrypt to hash passwords and UUID to generate unique user IDs can significantly improve the security of a web application.

Related Reads

Up next
How To Use AI Tools or ChatGPT for Automating Job Search | Learn
Career Talk
Watch →