2016-09-25 4 views
1

まず、私がキャプチャ・ザ・フラッグ・コンテストに参加していると言いますが、Go Gorillaセッションに関連する質問にはいくつかの問題があります。私はGoでコーディングしたことがないので、これは楽しく不満です。ゴリラセッションの復号化Cookieデータ

私は秘密鍵を持っています。私はコード化されたCookieを持っています。私は、私が持っている秘密鍵を使用して、クッキーを解読し、そこにあるデータを編集し、変更されたデータで再暗号化してチャレンジを進める必要があります。

私はGorilla Sessions Packageのドキュメントを読んだことがあります。

誰でも手助けできますか?どこから始めますか?

+1

あなたはゴリラのセッションで使用される 'securecookie'ライブラリ内のコーデックの実装を見たことがありますか? https://github.com/gorilla/securecookie/blob/master/securecookie.go –

+0

SecureCookieとは対照的に、Gorilla Sessionsを見るように指示してくれたので、私は当初は却下しました。しかし、そのパッケージを使用するのは理にかなっているので、共有してくれてありがとう! –

答えて

1

docs - ゴリラを見るとsecure cookie packageがあります。 アプリのアーキテクチャに応じて、基本的な実装は次のように動作します:

アプリで使用するセッション管理パッケージを作成します。例のために - sessionmngr

の中にsessionmngrをインポートすると、"github.com/gorilla/securecookie"がインポートされます。

sessionmngrパッケージでは、小文字のinit()を使用して、securecookieのプライベートインスタンスを設定します。パッケージがインポートされると、小文字のinit()関数が宣言された順に呼び出されます。 (詳細は言語specをご覧ください)。このインスタンスを使用して、標準ライブラリのhttp.RequestからのCookieをエンコードおよびデコードします。

import (
    "github.com/gorilla/securecookie"  

    //you will need this later 
    "http" 
) 

//declare private secure cookie 
var s *securecookie.SecureCookie 

//initialize it here (taken from the gorilla docs example) 
func init() { 
    var hashKey = []byte("very-secret") 
    var blockKey = []byte("a-lot-secret") 
    s = securecookie.New(hashKey, blockKey) 
} 

その後、エンコードおよびクッキーの値をデコードするために必要な機能では、パッケージ全体sを使用します。 securecookie package documentationには定型的な例があります。

すでに暗号化されたCookieの読み取りと変更の要件を満たすには、上記の例で設定したsecurecookieのインスタンスのDecodeEncodeメソッドを使用します。

何かのように---

func DecodeAndModify(w http.ResponseWriter, r *http.Request) { 
    //get reference to cookie if set 
    if cookie, err := r.Cookie("cookie-name"); err == nil { 

     value := make(map[string]string) 
     //use Decode to get the value from the cookie 
     if err = s.Decode("cookie-name", cookie.Value, &value); err == nil { 
      //modify the value in some way 
      value["newKey"] = "newValue" 
      //re-encode it 
      if encoded, err := s.Encode("cookie-name", value); err == nil { 
       cookie := &http.Cookie{ 
        Name: "cookie-name", 
        Value: encoded, 
        Path: "/", 
       } 
       http.SetCookie(w, cookie) 
      } 
     } 
    } 
} 
+0

シラバスに感謝、詳細な答えをいただきありがとうございます。私はこれがまさに私が必要としているものだと信じています。それで、正解を感謝の印としてマークします。 –

関連する問題