Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 470
- Log:
Version 1.0.2 with updated reCaptcha
- Author:
- rool
- Date:
- Sat Apr 16 11:20:56 +0100 2016
- Size:
- 1406 Bytes
1 | require 'json' |
2 | require 'recaptcha' |
3 | require 'base64' |
4 | require 'securerandom' |
5 | require 'openssl' |
6 | |
7 | # module Base64 |
8 | # class << self |
9 | # def urlsafe_encode64(bin) |
10 | # [bin].pack("m0").tr("+/", "-_") |
11 | # end |
12 | # end |
13 | # end |
14 | |
15 | module Recaptcha |
16 | module Token |
17 | |
18 | # Ruby 1.8.7 lacks SecureRandom#uuid and Base64#urlsafe_encode64; |
19 | # it also doesn't support packing by "m0", so inserts newlines into |
20 | # encoded strings that we need to strip c.f. the Ruby 1.9.1 code |
21 | # under the hood of #urlsafe_encode64. |
22 | # |
23 | def self.uuid |
24 | ary = SecureRandom.random_bytes(16).unpack("NnnnnN") |
25 | ary[2] = (ary[2] & 0x0fff) | 0x4000 |
26 | ary[3] = (ary[3] & 0x3fff) | 0x8000 |
27 | "%08x-%04x-%04x-%04x-%04x%08x" % ary |
28 | end |
29 | |
30 | def self.urlsafe_encode64(bin) |
31 | [bin].pack('m').tr("\n",'').tr('+/', '-_') |
32 | end |
33 | |
34 | def self.secure_token |
35 | private_key = Recaptcha.configuration.private_key |
36 | raise RecaptchaError, "No private key specified." unless private_key |
37 | |
38 | stoken_json = {'session_id' => self.uuid, 'ts_ms' => (Time.now.to_f * 1000).to_i}.to_json |
39 | cipher = OpenSSL::Cipher::AES128.new(:ECB) |
40 | private_key_digest = Digest::SHA1.digest(private_key)[0...16] |
41 | |
42 | cipher.encrypt |
43 | cipher.key = private_key_digest |
44 | encrypted_stoken = cipher.update(stoken_json) << cipher.final |
45 | self.urlsafe_encode64(encrypted_stoken).gsub(/\=+\Z/, '') |
46 | end |
47 | end |
48 | end |