{"action":"created","comment":{"url":"https://api.github.com/repos/skycoin/skycoin/pulls/comments/158668087","pull_request_review_id":85510330,"id":158668087,"diff_hunk":"@@ -0,0 +1,173 @@\n+package cipher\n+\n+import (\n+\t\"bytes\"\n+\t\"encoding/base64\"\n+\t\"encoding/binary\"\n+\t\"errors\"\n+\t\"strings\"\n+)\n+\n+const (\n+\t// the data size of each block\n+\tblockSize = 32 // 32 bytes\n+\t// the data length prefix size\n+\tlenPrefixSize = 4 // 4 bytes\n+)\n+\n+var (\n+\t// ErrInvalidPassword represents the invalid password error\n+\tErrInvalidPassword = errors.New(\"invalid password\")\n+)\n+\n+// Encrypt encrypts the data with password\n+// 1> SHA256 the data\n+// 2> Add 32 bits length prefix to indicate the length of data, including the data hash\n+// 3> Split data into 256 bits(32 bytes) blocks (pad to 32 bytes with nulls at end)\n+// 4> Each block is encrypted by XORing the unencrypted block with SHA256(SHA256(password), SHA256(index, SHA256(nonce))\n+// \t  - index is 0 for the first block of 32 bytes, 1 for the second block of 32 bytes, 2 for third block\n+// 5> SHA256 the nonce with comma seperated, hex encoded blocks of 32 bytes(256 bits)\n+// 6> Encode <checksum(32 bytes)><nonce(32 bytes)><block0.Hex(), block1.Hex()...> with base64\n+func Encrypt(data []byte, password []byte) ([]byte, error) {\n+\t// set data length prefix\n+\tdataHash := SumSHA256(data)\n+\tlength := make([]byte, lenPrefixSize)\n+\tbinary.PutUvarint(length, uint64(len(data)+32)) // the length including the data hash\n+\tvar pbuf bytes.Buffer\n+\tpbuf.Write(length)\n+\tpbuf.Write(dataHash[:])\n+\tpbuf.Write(data)\n+\tpdata := pbuf.Bytes()\n+\n+\t// split the data into 256 bit blocks(pad with null to 32 bytes)\n+\tl := len(pdata) // hash length + data length\n+\tn := l / blockSize\n+\tvar blocks [][blockSize]byte\n+\tfor i := 0; i < n; i++ {\n+\t\tvar b [blockSize]byte\n+\t\tcopy(b[:], pdata[i*blockSize:(i+1)*blockSize])\n+\t\tblocks = append(blocks, b)\n+\t}\n+\n+\t// append last block if exist\n+\tif l%blockSize > 0 {\n+\t\tb := [blockSize]byte{}\n+\t\tcopy(b[:], pdata[n*blockSize:])\n+\t\tblocks = append(blocks, b)\n+\t}\n+\n+\tnonce := RandByte(blockSize)\n+\tvar encryptedBlocks []string\n+\t// encode the blocks\n+\tfor i := range blocks {\n+\t\t// hash(password, hash(index, hash(nonce)))\n+\t\th := hashPwdIndexNonce(password, int64(i), nonce)\n+\t\tbh := SHA256(blocks[i])\n+\t\tencryptedHash := bh.Xor(h)\n+\t\t// encode the encrypted hash in base64\n+\t\ts := base64.StdEncoding.EncodeToString(encryptedHash[:])\n+\t\tencryptedBlocks = append(encryptedBlocks, s)\n+\t}\n+\n+\tencryptedData := strings.Join(encryptedBlocks, \",\")\n+\tnonceAndDataBytes := append(nonce, []byte(encryptedData)...)\n+\tcheckSum := SumSHA256(nonceAndDataBytes)\n+\tvar buf bytes.Buffer\n+\t_, err := buf.Write(checkSum[:])\n+\tif err != nil {\n+\t\treturn nil, err\n+\t}\n+\n+\t_, err = buf.Write(nonceAndDataBytes)\n+\tif err != nil {\n+\t\treturn nil, err\n+\t}\n+\n+\treturn buf.Bytes(), nil\n+}\n+\n+// Decrypt decrypts the data\n+func Decrypt(data []byte, password []byte) ([]byte, error) {\n+\tbuf := bytes.NewBuffer(data)\n+\n+\tcheckSumBytes := make([]byte, blockSize)\n+\tn, err := buf.Read(checkSumBytes)\n+\tif err != nil {\n+\t\treturn nil, err\n+\t}\n+\n+\tif n != blockSize {\n+\t\treturn nil, errors.New(\"decode checksum failed\")\n+\t}\n+\n+\tvar checkSum SHA256\n+\tcopy(checkSum[:], checkSumBytes)","path":"src/cipher/encrypt.go","position":104,"original_position":104,"commit_id":"2ec7559fe3bcf9888e1161efc2cd3d7059f2ad6c","original_commit_id":"2ec7559fe3bcf9888e1161efc2cd3d7059f2ad6c","user":{"login":"gz-c","id":26845312,"avatar_url":"https://avatars0.githubusercontent.com/u/26845312?v=4","gravatar_id":"","url":"https://api.github.com/users/gz-c","html_url":"https://github.com/gz-c","followers_url":"https://api.github.com/users/gz-c/followers","following_url":"https://api.github.com/users/gz-c/following{/other_user}","gists_url":"https://api.github.com/users/gz-c/gists{/gist_id}","starred_url":"https://api.github.com/users/gz-c/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gz-c/subscriptions","organizations_url":"https://api.github.com/users/gz-c/orgs","repos_url":"https://api.github.com/users/gz-c/repos","events_url":"https://api.github.com/users/gz-c/events{/privacy}","received_events_url":"https://api.github.com/users/gz-c/received_events","type":"User","site_admin":false},"body":"base64 decode?","created_at":"2017-12-26T05:15:09Z","updated_at":"2017-12-26T05:18:11Z","html_url":"https://github.com/skycoin/skycoin/pull/698#discussion_r158668087","pull_request_url":"https://api.github.com/repos/skycoin/skycoin/pulls/698","author_association":"OWNER","_links":{"self":{"href":"https://api.github.com/repos/skycoin/skycoin/pulls/comments/158668087"},"html":{"href":"https://github.com/skycoin/skycoin/pull/698#discussion_r158668087"},"pull_request":{"href":"https://api.github.com/repos/skycoin/skycoin/pulls/698"}}},"pull_request":{"url":"https://api.github.com/repos/skycoin/skycoin/pulls/698","id":155781904,"html_url":"https://github.com/skycoin/skycoin/pull/698","diff_url":"https://github.com/skycoin/skycoin/pull/698.diff","patch_url":"https://github.com/skycoin/skycoin/pull/698.patch","issue_url":"https://api.github.com/repos/skycoin/skycoin/issues/698","number":698,"state":"open","locked":false,"title":"Encrypt seed and private keys in wallet","user":{"login":"iketheadore","id":2589926,"avatar_url":"https://avatars1.githubusercontent.com/u/2589926?v=4","gravatar_id":"","url":"https://api.github.com/users/iketheadore","html_url":"https://github.com/iketheadore","followers_url":"https://api.github.com/users/iketheadore/followers","following_url":"https://api.github.com/users/iketheadore/following{/other_user}","gists_url":"https://api.github.com/users/iketheadore/gists{/gist_id}","starred_url":"https://api.github.com/users/iketheadore/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/iketheadore/subscriptions","organizations_url":"https://api.github.com/users/iketheadore/orgs","repos_url":"https://api.github.com/users/iketheadore/repos","events_url":"https://api.github.com/users/iketheadore/events{/privacy}","received_events_url":"https://api.github.com/users/iketheadore/received_events","type":"User","site_admin":false},"body":"- Updated the version of wallet from \"0.1\" to \"0.2\", \r\n- Add `Encrypt` and `Password` fields in `wallet.Options`, create encrypted wallet if they are both provided.\r\n\r\nFix #664 ","created_at":"2017-12-01T04:44:18Z","updated_at":"2017-12-26T05:18:11Z","closed_at":null,"merged_at":null,"merge_commit_sha":"26ed0ca372987836f58275e77b7f46bfaa6cf071","assignee":null,"assignees":[],"requested_reviewers":[],"milestone":null,"commits_url":"https://api.github.com/repos/skycoin/skycoin/pulls/698/commits","review_comments_url":"https://api.github.com/repos/skycoin/skycoin/pulls/698/comments","review_comment_url":"https://api.github.com/repos/skycoin/skycoin/pulls/comments{/number}","comments_url":"https://api.github.com/repos/skycoin/skycoin/issues/698/comments","statuses_url":"https://api.github.com/repos/skycoin/skycoin/statuses/2ec7559fe3bcf9888e1161efc2cd3d7059f2ad6c","head":{"label":"iketheadore:encrypt-seed-and-privatekey","ref":"encrypt-seed-and-privatekey","sha":"2ec7559fe3bcf9888e1161efc2cd3d7059f2ad6c","user":{"login":"iketheadore","id":2589926,"avatar_url":"https://avatars1.githubusercontent.com/u/2589926?v=4","gravatar_id":"","url":"https://api.github.com/users/iketheadore","html_url":"https://github.com/iketheadore","followers_url":"https://api.github.com/users/iketheadore/followers","following_url":"https://api.github.com/users/iketheadore/following{/other_user}","gists_url":"https://api.github.com/users/iketheadore/gists{/gist_id}","starred_url":"https://api.github.com/users/iketheadore/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/iketheadore/subscriptions","organizations_url":"https://api.github.com/users/iketheadore/orgs","repos_url":"https://api.github.com/users/iketheadore/repos","events_url":"https://api.github.com/users/iketheadore/events{/privacy}","received_events_url":"https://api.github.com/users/iketheadore/received_events","type":"User","site_admin":false},"repo":{"id":67753739,"name":"skycoin","full_name":"iketheadore/skycoin","owner":{"login":"iketheadore","id":2589926,"avatar_url":"https://avatars1.githubusercontent.com/u/2589926?v=4","gravatar_id":"","url":"https://api.github.com/users/iketheadore","html_url":"https://github.com/iketheadore","followers_url":"https://api.github.com/users/iketheadore/followers","following_url":"https://api.github.com/users/iketheadore/following{/other_user}","gists_url":"https://api.github.com/users/iketheadore/gists{/gist_id}","starred_url":"https://api.github.com/users/iketheadore/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/iketheadore/subscriptions","organizations_url":"https://api.github.com/users/iketheadore/orgs","repos_url":"https://api.github.com/users/iketheadore/repos","events_url":"https://api.github.com/users/iketheadore/events{/privacy}","received_events_url":"https://api.github.com/users/iketheadore/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/iketheadore/skycoin","description":"Skycoin Client","fork":true,"url":"https://api.github.com/repos/iketheadore/skycoin","forks_url":"https://api.github.com/repos/iketheadore/skycoin/forks","keys_url":"https://api.github.com/repos/iketheadore/skycoin/keys{/key_id}","collaborators_url":"https://api.github.com/repos/iketheadore/skycoin/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/iketheadore/skycoin/teams","hooks_url":"https://api.github.com/repos/iketheadore/skycoin/hooks","issue_events_url":"https://api.github.com/repos/iketheadore/skycoin/issues/events{/number}","events_url":"https://api.github.com/repos/iketheadore/skycoin/events","assignees_url":"https://api.github.com/repos/iketheadore/skycoin/assignees{/user}","branches_url":"https://api.github.com/repos/iketheadore/skycoin/branches{/branch}","tags_url":"https://api.github.com/repos/iketheadore/skycoin/tags","blobs_url":"https://api.github.com/repos/iketheadore/skycoin/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/iketheadore/skycoin/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/iketheadore/skycoin/git/refs{/sha}","trees_url":"https://api.github.com/repos/iketheadore/skycoin/git/trees{/sha}","statuses_url":"https://api.github.com/repos/iketheadore/skycoin/statuses/{sha}","languages_url":"https://api.github.com/repos/iketheadore/skycoin/languages","stargazers_url":"https://api.github.com/repos/iketheadore/skycoin/stargazers","contributors_url":"https://api.github.com/repos/iketheadore/skycoin/contributors","subscribers_url":"https://api.github.com/repos/iketheadore/skycoin/subscribers","subscription_url":"https://api.github.com/repos/iketheadore/skycoin/subscription","commits_url":"https://api.github.com/repos/iketheadore/skycoin/commits{/sha}","git_commits_url":"https://api.github.com/repos/iketheadore/skycoin/git/commits{/sha}","comments_url":"https://api.github.com/repos/iketheadore/skycoin/comments{/number}","issue_comment_url":"https://api.github.com/repos/iketheadore/skycoin/issues/comments{/number}","contents_url":"https://api.github.com/repos/iketheadore/skycoin/contents/{+path}","compare_url":"https://api.github.com/repos/iketheadore/skycoin/compare/{base}...{head}","merges_url":"https://api.github.com/repos/iketheadore/skycoin/merges","archive_url":"https://api.github.com/repos/iketheadore/skycoin/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/iketheadore/skycoin/downloads","issues_url":"https://api.github.com/repos/iketheadore/skycoin/issues{/number}","pulls_url":"https://api.github.com/repos/iketheadore/skycoin/pulls{/number}","milestones_url":"https://api.github.com/repos/iketheadore/skycoin/milestones{/number}","notifications_url":"https://api.github.com/repos/iketheadore/skycoin/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/iketheadore/skycoin/labels{/name}","releases_url":"https://api.github.com/repos/iketheadore/skycoin/releases{/id}","deployments_url":"https://api.github.com/repos/iketheadore/skycoin/deployments","created_at":"2016-09-09T01:23:04Z","updated_at":"2016-10-28T11:29:26Z","pushed_at":"2017-12-25T13:48:31Z","git_url":"git://github.com/iketheadore/skycoin.git","ssh_url":"git@github.com:iketheadore/skycoin.git","clone_url":"https://github.com/iketheadore/skycoin.git","svn_url":"https://github.com/iketheadore/skycoin","homepage":null,"size":55564,"stargazers_count":1,"watchers_count":1,"language":"Go","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":null,"forks":0,"open_issues":0,"watchers":1,"default_branch":"develop"}},"base":{"label":"skycoin:develop","ref":"develop","sha":"e3678c0d3efe0b62b7ae56c361b897ce7ba6ddc3","user":{"login":"skycoin","id":29321691,"avatar_url":"https://avatars3.githubusercontent.com/u/29321691?v=4","gravatar_id":"","url":"https://api.github.com/users/skycoin","html_url":"https://github.com/skycoin","followers_url":"https://api.github.com/users/skycoin/followers","following_url":"https://api.github.com/users/skycoin/following{/other_user}","gists_url":"https://api.github.com/users/skycoin/gists{/gist_id}","starred_url":"https://api.github.com/users/skycoin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/skycoin/subscriptions","organizations_url":"https://api.github.com/users/skycoin/orgs","repos_url":"https://api.github.com/users/skycoin/repos","events_url":"https://api.github.com/users/skycoin/events{/privacy}","received_events_url":"https://api.github.com/users/skycoin/received_events","type":"Organization","site_admin":false},"repo":{"id":15427835,"name":"skycoin","full_name":"skycoin/skycoin","owner":{"login":"skycoin","id":29321691,"avatar_url":"https://avatars3.githubusercontent.com/u/29321691?v=4","gravatar_id":"","url":"https://api.github.com/users/skycoin","html_url":"https://github.com/skycoin","followers_url":"https://api.github.com/users/skycoin/followers","following_url":"https://api.github.com/users/skycoin/following{/other_user}","gists_url":"https://api.github.com/users/skycoin/gists{/gist_id}","starred_url":"https://api.github.com/users/skycoin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/skycoin/subscriptions","organizations_url":"https://api.github.com/users/skycoin/orgs","repos_url":"https://api.github.com/users/skycoin/repos","events_url":"https://api.github.com/users/skycoin/events{/privacy}","received_events_url":"https://api.github.com/users/skycoin/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/skycoin/skycoin","description":"Skycoin Core and Wallet","fork":false,"url":"https://api.github.com/repos/skycoin/skycoin","forks_url":"https://api.github.com/repos/skycoin/skycoin/forks","keys_url":"https://api.github.com/repos/skycoin/skycoin/keys{/key_id}","collaborators_url":"https://api.github.com/repos/skycoin/skycoin/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/skycoin/skycoin/teams","hooks_url":"https://api.github.com/repos/skycoin/skycoin/hooks","issue_events_url":"https://api.github.com/repos/skycoin/skycoin/issues/events{/number}","events_url":"https://api.github.com/repos/skycoin/skycoin/events","assignees_url":"https://api.github.com/repos/skycoin/skycoin/assignees{/user}","branches_url":"https://api.github.com/repos/skycoin/skycoin/branches{/branch}","tags_url":"https://api.github.com/repos/skycoin/skycoin/tags","blobs_url":"https://api.github.com/repos/skycoin/skycoin/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/skycoin/skycoin/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/skycoin/skycoin/git/refs{/sha}","trees_url":"https://api.github.com/repos/skycoin/skycoin/git/trees{/sha}","statuses_url":"https://api.github.com/repos/skycoin/skycoin/statuses/{sha}","languages_url":"https://api.github.com/repos/skycoin/skycoin/languages","stargazers_url":"https://api.github.com/repos/skycoin/skycoin/stargazers","contributors_url":"https://api.github.com/repos/skycoin/skycoin/contributors","subscribers_url":"https://api.github.com/repos/skycoin/skycoin/subscribers","subscription_url":"https://api.github.com/repos/skycoin/skycoin/subscription","commits_url":"https://api.github.com/repos/skycoin/skycoin/commits{/sha}","git_commits_url":"https://api.github.com/repos/skycoin/skycoin/git/commits{/sha}","comments_url":"https://api.github.com/repos/skycoin/skycoin/comments{/number}","issue_comment_url":"https://api.github.com/repos/skycoin/skycoin/issues/comments{/number}","contents_url":"https://api.github.com/repos/skycoin/skycoin/contents/{+path}","compare_url":"https://api.github.com/repos/skycoin/skycoin/compare/{base}...{head}","merges_url":"https://api.github.com/repos/skycoin/skycoin/merges","archive_url":"https://api.github.com/repos/skycoin/skycoin/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/skycoin/skycoin/downloads","issues_url":"https://api.github.com/repos/skycoin/skycoin/issues{/number}","pulls_url":"https://api.github.com/repos/skycoin/skycoin/pulls{/number}","milestones_url":"https://api.github.com/repos/skycoin/skycoin/milestones{/number}","notifications_url":"https://api.github.com/repos/skycoin/skycoin/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/skycoin/skycoin/labels{/name}","releases_url":"https://api.github.com/repos/skycoin/skycoin/releases{/id}","deployments_url":"https://api.github.com/repos/skycoin/skycoin/deployments","created_at":"2013-12-25T01:50:16Z","updated_at":"2017-12-25T15:55:15Z","pushed_at":"2017-12-25T20:29:25Z","git_url":"git://github.com/skycoin/skycoin.git","ssh_url":"git@github.com:skycoin/skycoin.git","clone_url":"https://github.com/skycoin/skycoin.git","svn_url":"https://github.com/skycoin/skycoin","homepage":"","size":55290,"stargazers_count":267,"watchers_count":267,"language":"Go","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":123,"mirror_url":null,"archived":false,"open_issues_count":134,"license":null,"forks":123,"open_issues":134,"watchers":267,"default_branch":"develop"}},"_links":{"self":{"href":"https://api.github.com/repos/skycoin/skycoin/pulls/698"},"html":{"href":"https://github.com/skycoin/skycoin/pull/698"},"issue":{"href":"https://api.github.com/repos/skycoin/skycoin/issues/698"},"comments":{"href":"https://api.github.com/repos/skycoin/skycoin/issues/698/comments"},"review_comments":{"href":"https://api.github.com/repos/skycoin/skycoin/pulls/698/comments"},"review_comment":{"href":"https://api.github.com/repos/skycoin/skycoin/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/skycoin/skycoin/pulls/698/commits"},"statuses":{"href":"https://api.github.com/repos/skycoin/skycoin/statuses/2ec7559fe3bcf9888e1161efc2cd3d7059f2ad6c"}},"author_association":"MEMBER"}}
{"actor":{"display_login":"gz-c"}}