tclserv

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

0004-hashing.tcl (2731B)


      1 #! /usr/bin/env tclsh
      2 
      3 # 
      4 # This file is part of the password-- distribution (https://github.com/xxxx or http://xxx.github.io).
      5 # Copyright (c) 2016 Ellenor Bjornsdottir
      6 # 
      7 # This file is free software - you may distribute it under the M.I.T. license.
      8 # If included with GPL'd software, this file is instead available under the terms of
      9 # the GPL, of the version relevant to the whole.
     10 # Permission is hereby granted, free of charge, to any person obtaining a copy
     11 # of this software and associated documentation files (the "Software"), to deal
     12 # in the Software without restriction, including without limitation the rights
     13 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     14 # copies of the Software, and to permit persons to whom the Software is
     15 # furnished to do so, subject to the following conditions:
     16 # 
     17 # The above copyright notice and this permission notice shall be included in
     18 # all copies or substantial portions of the Software.
     19 #
     20 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     21 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     22 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     23 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     24 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     25 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     26 # THE SOFTWARE.
     27 
     28 #package require Expect
     29 package require base64
     30 package require aes
     31 package require sha256
     32 
     33 proc pad {origlen {mult 16}} {
     34  set next [expr $origlen/$mult+1]
     35  set nextl [expr ${next}*${mult}]
     36  set padlen [expr ${nextl}-${origlen}]
     37  return $padlen
     38 }
     39 
     40 proc encrypt {site pass} {
     41  set inited [::aes::Init ecb [::sha2::sha256 -bin -- [join [list $site $pass] ":"]] "aaaaaaaaaaaaaaaa"]
     42  set padout [pad [string length $site]]
     43  append site [string repeat \0 $padout]
     44  set encd [::aes::Encrypt $inited [::sha2::sha256 -bin -- $pass]]
     45  ::aes::Final $inited
     46  return [encrypt-v1 $site $encd]
     47 }
     48 
     49 proc encrypt-v1 {site pass} {
     50  set inited [::aes::Init ecb [::sha2::sha256 -bin -- $pass] "aaaaaaaaaaaaaaaa"]
     51  set padout [pad [string length $site]]
     52  append site [string repeat \0 $padout]
     53  set encd [::aes::Encrypt $inited $site]
     54  ::aes::Final $inited
     55  return $encd
     56 }
     57 
     58 proc pwhash.SSHA256 {pass {site "a"}} {
     59  return [format "SSHA256/%s/%s" $site [string map {/ - + _ = {}} [::base64::encode -maxlen 0 -wrapchar "" [encrypt $site $pass]]]]
     60 }
     61 
     62 proc pwhash {args} {
     63  if {[llength $args] == 1} {lassign $args pass; set alg SSHA256; set salt a}
     64  if {[llength $args] == 2} {lassign $args pass salt; set alg SSHA256}
     65  if {[llength $args] == 3} {lassign $args alg pass salt}
     66  return [pwhash.$alg $pass $salt]
     67 }