ppp 0.1.6.alpha → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/ppp.rb +7 -2
- data/lib/ppp/card/base.rb +42 -5
- data/lib/ppp/card/plain.rb +15 -4
- data/lib/ppp/generator.rb +4 -4
- data/lib/ppp/version.rb +1 -1
- metadata +5 -4
data/lib/ppp.rb
CHANGED
@@ -17,6 +17,7 @@ module Ppp
|
|
17
17
|
Generator.new key, opts
|
18
18
|
end
|
19
19
|
|
20
|
+
# @param [String] str some string to create a SHA-256 digest of
|
20
21
|
# @return a SHA-256 digest of the given string
|
21
22
|
def key_from_string str
|
22
23
|
Digest::SHA256.new.update( str ).to_s
|
@@ -27,8 +28,11 @@ module Ppp
|
|
27
28
|
Cppp.random_key
|
28
29
|
end
|
29
30
|
|
30
|
-
|
31
|
-
|
31
|
+
# @param [Symbol] type the type of card to return
|
32
|
+
# @param [Array] args parameters to pass to the card's constructor
|
33
|
+
# @return a +Card+ of the given type
|
34
|
+
def card type, *args
|
35
|
+
case type
|
32
36
|
when :html then return Card::Html.new *args
|
33
37
|
when :xml then return Card::Xml.new *args
|
34
38
|
when :plain then return Card::Plain.new *args
|
@@ -36,6 +40,7 @@ module Ppp
|
|
36
40
|
raise ArgumentError.new( "%s is not a valid printer style." % style )
|
37
41
|
end
|
38
42
|
|
43
|
+
# @return [Hash{Symbol => String}] the default alphabets
|
39
44
|
def default_alphabets
|
40
45
|
@@ALPHABETS
|
41
46
|
end
|
data/lib/ppp/card/base.rb
CHANGED
@@ -2,6 +2,7 @@ require 'ppp/generator'
|
|
2
2
|
|
3
3
|
module Ppp
|
4
4
|
module Card
|
5
|
+
# @abstract Subclass and override {#to_s} to implement a custom Card class.
|
5
6
|
class Base
|
6
7
|
attr_reader :card_number, :row_count, :title
|
7
8
|
|
@@ -13,6 +14,11 @@ module Ppp
|
|
13
14
|
@@ERROR_LONG_CODES = %[Passcodes longer than 16 characters are too long for printing]
|
14
15
|
@@ERROR_WRONG_NUM_ARGS = %[Wrong number of arguments. Expected %s, got %d]
|
15
16
|
|
17
|
+
# @param [Generator] generator the generator this card will get passcodes from
|
18
|
+
# @param [Hash] opts options to create the card with
|
19
|
+
# @option opts [Fixnum] :row_count (10) the number of rows in the card
|
20
|
+
# @option opts [String] :card_title ('PPP Passcard') the title of the card
|
21
|
+
# @option opts [Fixnum] :first_card_number (1) the number of the first card to create
|
16
22
|
def initialize generator, opts={}
|
17
23
|
@generator = generator
|
18
24
|
raise ArgumentError.new( @@ERROR_LONG_CODES ) if code_length > 16
|
@@ -26,23 +32,31 @@ module Ppp
|
|
26
32
|
@card_number = options[ :first_card_number ]
|
27
33
|
end
|
28
34
|
|
35
|
+
# @return [Fixnum] the number of characters in each passcode in the card
|
29
36
|
def code_length
|
30
37
|
@generator.length
|
31
38
|
end
|
32
39
|
|
40
|
+
# @param [Fixnum] number the new card number. Must be +1+ or more. The card number determines which passcodes
|
41
|
+
# will be printed on the card. For example, if the card has 10 rows, and each row has five passcodes, then
|
42
|
+
# Card #1 will retrieve passcodes 0-49 from the generator. If +card_number == 2+, then passcodes 50-99 will
|
43
|
+
# be printed from the generator.
|
33
44
|
def card_number= number
|
34
45
|
raise ArgumentError.new( "card number must be a positive integer" ) if number < 1
|
35
46
|
@card_number = number
|
36
47
|
end
|
37
48
|
|
49
|
+
# @return [Fixnum] the number of passcodes to be printed on each line
|
38
50
|
def passcodes_per_line
|
39
51
|
@passcodes_per_line ||= ( (@@CHARS_PER_LINE+1) / (code_length + 1) ).to_i
|
40
52
|
end
|
41
53
|
|
54
|
+
# @return [Fixnum] the number of passcodes to be printed on the card
|
42
55
|
def passcodes_per_card
|
43
56
|
passcodes_per_line * row_count
|
44
57
|
end
|
45
58
|
|
59
|
+
# @return [Array(Array(String))] the passcodes on the current card
|
46
60
|
def codes
|
47
61
|
(1..row_count).collect do |row|
|
48
62
|
offset = card_offset + ((row-1) * passcodes_per_line)
|
@@ -51,6 +65,13 @@ module Ppp
|
|
51
65
|
end
|
52
66
|
end
|
53
67
|
|
68
|
+
# @overload passcode(cell)
|
69
|
+
# @return [String] the passcode in the given cell
|
70
|
+
# @param [String] cell A cell ID. example: +10B+
|
71
|
+
# @overload passcode(row, column)
|
72
|
+
# @return [String] the passcode in the given row and column
|
73
|
+
# @param [Fixnum] row the row of the passcode to return
|
74
|
+
# @param [String] column the column of the passcode to return
|
54
75
|
def passcode *args
|
55
76
|
case args.size
|
56
77
|
when 1
|
@@ -72,21 +93,37 @@ module Ppp
|
|
72
93
|
@generator.passcode offset
|
73
94
|
end
|
74
95
|
|
96
|
+
# @overload verify(cell, given_passcode)
|
97
|
+
# @return [Boolean] +true+ if the given passcode matches the passcode in the given cell
|
98
|
+
# @param [String] cell A cell ID. example: +10B+
|
99
|
+
# @param [String] given_passcode the passcode to verify
|
100
|
+
# @overload verify(row, column, given_passcode)
|
101
|
+
# @return [Boolean] +true+ if the given passcode matches the passcode in the given row and column
|
102
|
+
# @param [Fixnum] row the row of the passcode to return
|
103
|
+
# @param [String] column the column of the passcode to return
|
104
|
+
# @param [String] given_passcode the passcode to verify
|
75
105
|
def verify *args
|
76
106
|
given_code = args.pop
|
77
107
|
given_code == passcode( *args )
|
78
108
|
end
|
79
109
|
|
80
|
-
def
|
81
|
-
|
110
|
+
def to_s
|
111
|
+
raise "unimplemented"
|
112
|
+
end
|
113
|
+
|
114
|
+
protected
|
115
|
+
|
116
|
+
def row_label row_index
|
117
|
+
"#{row_index + 1}:"
|
82
118
|
end
|
83
119
|
|
84
120
|
def column_label column_number
|
85
121
|
(@@FIRST_COLUMN.ord + (column_number - 1)).chr
|
86
122
|
end
|
87
|
-
|
88
|
-
|
89
|
-
|
123
|
+
|
124
|
+
# @return [Fixnum] the offset of passcodes to request from the generator for the given card. For example, if this
|
125
|
+
# card contains 50 passcodes, and we are currently on card 3, we want to request passcodes 100-149 from the
|
126
|
+
# generator.
|
90
127
|
def card_offset
|
91
128
|
(card_number-1) * passcodes_per_card
|
92
129
|
end
|
data/lib/ppp/card/plain.rb
CHANGED
@@ -15,35 +15,37 @@ class Ppp::Card::Plain < Ppp::Card::Base
|
|
15
15
|
line( split_bar )
|
16
16
|
end
|
17
17
|
|
18
|
-
def card_number= number
|
19
|
-
super
|
20
|
-
end
|
21
|
-
|
22
18
|
private
|
23
19
|
|
20
|
+
# @return [String] a horizontal rule
|
24
21
|
def bar
|
25
22
|
'-' * (width + 2)
|
26
23
|
end
|
27
24
|
|
25
|
+
# @return [Fixnum] the width of the table
|
28
26
|
def width
|
29
27
|
return rows.first.join( ' ' ).size if rows.size >= 1
|
30
28
|
header.size
|
31
29
|
end
|
32
30
|
|
31
|
+
# @return [Array(Array(String))] The cells of the table, with the first column containing the row labels
|
33
32
|
def rows
|
34
33
|
codes.each_with_index.collect { |row, i| [ first_column( i ) ] + row }
|
35
34
|
end
|
36
35
|
|
36
|
+
# @return [String] the column labels
|
37
37
|
def header
|
38
38
|
header_cells.join ' '
|
39
39
|
end
|
40
40
|
|
41
|
+
# @return [Array(String)] a list of the column headers
|
41
42
|
def header_cells
|
42
43
|
cells = passcodes_per_line.times.collect { |i| column_label(i+1).center code_length }
|
43
44
|
|
44
45
|
[' ' * first_column_width ] + cells
|
45
46
|
end
|
46
47
|
|
48
|
+
# @return [String] a line containing the title and card number
|
47
49
|
def title_str
|
48
50
|
title = @title[0, width - card_label.size] # trim title if it's too long to fit
|
49
51
|
|
@@ -51,31 +53,40 @@ class Ppp::Card::Plain < Ppp::Card::Base
|
|
51
53
|
title = title + (' ' * blank_space) + card_label
|
52
54
|
end
|
53
55
|
|
56
|
+
# @return [String] a label containing the card number
|
54
57
|
def card_label
|
55
58
|
"[#{card_number}]"
|
56
59
|
end
|
57
60
|
|
61
|
+
# @return [String] a horizontal rule with notches in it to deliniate the columns
|
58
62
|
def split_bar
|
59
63
|
parts = ['-' * first_column_width] + (1..passcodes_per_line).collect { '-' * code_length }
|
60
64
|
"-#{parts.join '+'}-"
|
61
65
|
end
|
62
66
|
|
67
|
+
# @return [Array(String)] a list of rows, each element containing the string of passcodes for that rows
|
63
68
|
def row_lines
|
64
69
|
rows.collect{ |r| line :pad, r.join( ' ' ) }
|
65
70
|
end
|
66
71
|
|
72
|
+
# @return [String] a line with the table's left and right border
|
73
|
+
# @param [String] str the content of the line
|
74
|
+
# @param [Symbol] +:pad+ if borders should have a one-space padding
|
67
75
|
def line pad=:nopad, str
|
68
76
|
return "| #{str} |\n" if pad == :pad
|
69
77
|
|
70
78
|
"|#{str}|\n"
|
71
79
|
end
|
72
80
|
|
81
|
+
# @return [String] the content of the first cell of the given row. The row's label
|
82
|
+
# @param [Fixnum] The index of the given row.
|
73
83
|
def first_column row_index
|
74
84
|
label = row_label row_index
|
75
85
|
padding = ' ' * (first_column_width - label.size)
|
76
86
|
padding + label
|
77
87
|
end
|
78
88
|
|
89
|
+
# @return [Fixnum] the required number of characters needed to fit the row label of each row
|
79
90
|
def first_column_width
|
80
91
|
@row_count.to_s.size + 1
|
81
92
|
end
|
data/lib/ppp/generator.rb
CHANGED
@@ -8,10 +8,10 @@ class Ppp::Generator
|
|
8
8
|
|
9
9
|
# @param [String] sha256_key a 64 hex-digit string representation of a
|
10
10
|
# SHA-256 hash. This hash will seed all the generated passcodes.
|
11
|
-
# @param [
|
12
|
-
# @
|
13
|
-
#
|
14
|
-
# will be removed.
|
11
|
+
# @param [Hash] opts the options to create the generator with.
|
12
|
+
# @option opts [Fixnum] :length (4) the number of characters in each generated passcode
|
13
|
+
# @option opts [String] :alphabet (:conservative) a string containing the characters passcodes will
|
14
|
+
# be comprised of. Cannot contain null characters and duplicate characters will be removed.
|
15
15
|
def initialize sha256_key, opts={}
|
16
16
|
raise NotHexKey.new( sha256_key ) if @@HEX_PATTERN.match( sha256_key ).nil?
|
17
17
|
|
data/lib/ppp/version.rb
CHANGED
metadata
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ppp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
5
|
-
prerelease:
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Jon Sangster
|
@@ -52,9 +52,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
52
52
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
53
|
none: false
|
54
54
|
requirements:
|
55
|
-
- - ! '
|
55
|
+
- - ! '>='
|
56
56
|
- !ruby/object:Gem::Version
|
57
|
-
version:
|
57
|
+
version: '0'
|
58
58
|
requirements: []
|
59
59
|
rubyforge_project: ppp
|
60
60
|
rubygems_version: 1.8.15
|
@@ -63,3 +63,4 @@ specification_version: 3
|
|
63
63
|
summary: Ruby bindings for John Graham-Cumming's implementation of GRC's Perfect Paper
|
64
64
|
Passwords
|
65
65
|
test_files: []
|
66
|
+
has_rdoc:
|