Skip to content

Commit

Permalink
ハッシュを求めるときにopensslを使わない
Browse files Browse the repository at this point in the history
  • Loading branch information
Gurrium committed Dec 2, 2023
1 parent 9a8c878 commit 2a397bb
Showing 1 changed file with 4 additions and 16 deletions.
20 changes: 4 additions & 16 deletions webapp/go/app.go
Expand Up @@ -2,6 +2,7 @@ package main

import (
crand "crypto/rand"
"crypto/sha512"
"encoding/json"
"fmt"
"html/template"
Expand All @@ -10,7 +11,6 @@ import (
"net/http"
"net/url"
"os"
"os/exec"
"path"
"regexp"
"runtime"
Expand Down Expand Up @@ -130,22 +130,10 @@ func validateUser(accountName, password string) bool {
regexp.MustCompile(`\A[0-9a-zA-Z_]{6,}\z`).MatchString(password)
}

// 今回のGo実装では言語側のエスケープの仕組みが使えないのでOSコマンドインジェクション対策できない
// 取り急ぎPHPのescapeshellarg関数を参考に自前で実装
// cf: http://jp2.php.net/manual/ja/function.escapeshellarg.php
func escapeshellarg(arg string) string {
return "'" + strings.Replace(arg, "'", "'\\''", -1) + "'"
}

func digest(src string) string {
// opensslのバージョンによっては (stdin)= というのがつくので取る
out, err := exec.Command("/bin/bash", "-c", `printf "%s" `+escapeshellarg(src)+` | openssl dgst -sha512 | sed 's/^.*= //'`).Output()
if err != nil {
log.Print(err)
return ""
}

return strings.TrimSuffix(string(out), "\n")
h := sha512.New()
h.Write([]byte(src))
return fmt.Sprintf("%x", h.Sum(nil))
}

func calculateSalt(accountName string) string {
Expand Down

0 comments on commit 2a397bb

Please sign in to comment.