2017-05-06 21:21:39 +02:00
package crypto
import (
"crypto/md5"
2017-05-06 22:14:02 +02:00
"crypto/rand"
2017-05-06 21:21:39 +02:00
"fmt"
2017-09-18 17:21:48 +02:00
"io"
2017-05-06 21:21:39 +02:00
"strings"
)
2017-05-26 12:12:52 +02:00
// GenerateMD5Hash : Generate a md5 hash from a string
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
func GenerateMD5Hash ( str string ) ( string , error ) {
str = strings . ToLower ( strings . TrimSpace ( str ) )
2017-05-06 21:21:39 +02:00
hash := md5 . New ( )
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
_ , err := hash . Write ( [ ] byte ( str ) )
if err != nil {
return "" , err
}
return fmt . Sprintf ( "%x" , hash . Sum ( nil ) ) , nil
2017-05-06 21:21:39 +02:00
}
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
2017-05-26 12:12:52 +02:00
// GenerateRandomToken16 : Generates a random token 16bits long
2017-05-06 21:21:39 +02:00
func GenerateRandomToken16 ( ) ( string , error ) {
return GenerateRandomToken ( 16 )
}
2017-05-26 12:12:52 +02:00
// GenerateRandomToken32 : Generates a random token 32bits long
2017-05-06 21:21:39 +02:00
func GenerateRandomToken32 ( ) ( string , error ) {
return GenerateRandomToken ( 32 )
}
2017-05-26 12:12:52 +02:00
// GenerateRandomToken : Generates a random token int n long
2017-05-06 21:21:39 +02:00
func GenerateRandomToken ( n int ) ( string , error ) {
token := make ( [ ] byte , n )
2017-09-18 17:21:48 +02:00
_ , err := io . ReadFull ( rand . Reader , token )
2017-05-06 21:21:39 +02:00
// %x base 16, lower-case, two characters per byte
return fmt . Sprintf ( "%x" , token ) , err
}