Class: Shugoi::Pow
- Inherits:
-
Object
- Object
- Shugoi::Pow
- Defined in:
- lib/shugoi/pow.rb
Overview
Proof-of-work anti-curl. Parité avec core.ts (module Node) :
salt = HMAC(secret, ts)
proof = "ts:nonce" où SHA256(salt:nonce) a >= POW_DIFFICULTY bits à zéro en tête.
Instance Method Summary collapse
-
#challenge ⇒ Object
Génère le challenge à injecter (window.__sg_pow).
-
#initialize(signing_secret, difficulty = 10, ttl_ms = 120_000) ⇒ Pow
constructor
A new instance of Pow.
-
#valid?(proof) ⇒ Boolean
Vérifie un proof "ts:nonce".
Constructor Details
#initialize(signing_secret, difficulty = 10, ttl_ms = 120_000) ⇒ Pow
Returns a new instance of Pow.
9 10 11 12 13 |
# File 'lib/shugoi/pow.rb', line 9 def initialize(signing_secret, difficulty = 10, ttl_ms = 120_000) @secret = signing_secret.to_s @difficulty = difficulty @ttl_ms = ttl_ms end |
Instance Method Details
#challenge ⇒ Object
Génère le challenge à injecter (window.__sg_pow).
16 17 18 19 |
# File 'lib/shugoi/pow.rb', line 16 def challenge ts = Utils.now_sec { ts: ts, salt: salt(ts), difficulty: @difficulty } end |
#valid?(proof) ⇒ Boolean
Vérifie un proof "ts:nonce".
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/shugoi/pow.rb', line 22 def valid?(proof) return false if proof.to_s.empty? || @secret.empty? ts_str, solution = proof.to_s.split(":", 2) return false if ts_str.nil? || solution.nil? ts = ts_str.to_i return false if ts.zero? return false if (Utils.now_ms - ts * 1000).abs > @ttl_ms digest = Utils.sha256_hex("#{salt(ts_str)}:#{solution}") Utils.leading_zero_bits(digest) >= @difficulty end |