Class: OpenSSL::BN
- Inherits:
-
Object
- Object
- OpenSSL::BN
- Includes:
- Comparable
- Defined in:
- lib/openssl/bn.rb,
ext/openssl/ossl_bn.c
Class Method Summary collapse
- .generate_prime ⇒ Object
-
.pseudo_rand ⇒ Object
BN.pseudo_rand(bits [, fill [, odd]]) -> aBN.
- .pseudo_rand_range(range) ⇒ Object
-
.rand ⇒ Object
BN.rand(bits [, fill [, odd]]) -> aBN.
- .rand_range(range) ⇒ Object
Instance Method Summary collapse
- #%(bn2) ⇒ Object
- #*(bn2) ⇒ Object
- #**(bn2) ⇒ Object
- #+(bn2) ⇒ Object
- #-(bn2) ⇒ Object
-
#/(bn2) ⇒ Array
Division of OpenSSL::BN instances.
- #<<(bits) ⇒ Object
- #== ⇒ Object (also: #===)
- #>>(bits) ⇒ Object
-
#bit_set?(bit) ⇒ Boolean
Returns boolean of whether
bit
is set. - #clear_bit!(bit) ⇒ self
- #cmp(bn2) ⇒ Integer (also: #<=>)
- #coerce(other) ⇒ Object
- #copy ⇒ Object
-
#eql?(obj) ⇒ Boolean
Returns
true
only if obj is aOpenSSL::BN
with the same value as big. - #gcd(bn2) ⇒ Object
-
#hash ⇒ Integer
Returns a hash code for this object.
-
#initialize(*args) ⇒ Object
constructor
call-seq: OpenSSL::BN.new => aBN OpenSSL::BN.new(bn) => aBN OpenSSL::BN.new(integer) => aBN OpenSSL::BN.new(string) => aBN OpenSSL::BN.new(string, 0 | 2 | 10 | 16) => aBN.
- #lshift!(bits) ⇒ self
- #mask_bits! ⇒ Object
- #mod_add(bn1, bn2) ⇒ Object
- #mod_exp(bn1, bn2) ⇒ Object
- #mod_inverse(bn2) ⇒ Object
- #mod_mul(bn1, bn2) ⇒ Object
- #mod_sqr(bn2) ⇒ Object
- #mod_sub(bn1, bn2) ⇒ Object
- #num_bits ⇒ Integer
- #num_bytes ⇒ Integer
- #odd? ⇒ Boolean
- #one? ⇒ Boolean
- #pretty_print(q) ⇒ Object
-
#prime?(*args) ⇒ Boolean
Performs a Miller-Rabin probabilistic primality test with
checks
iterations. -
#prime_fasttest?(*args) ⇒ Boolean
Performs a Miller-Rabin primality test.
- #rshift!(bits) ⇒ self
- #set_bit!(bit) ⇒ self
- #sqr ⇒ Object
- #to_bn ⇒ Object
- #to_i ⇒ Integer (also: #to_int)
-
#to_s(*args) ⇒ Object
Parameters *
base
- integer Valid values: * 0 - MPI * 2 - binary * 10 - the default * 16 - hex. - #ucmp(bn2) ⇒ Integer
- #zero? ⇒ Boolean
Constructor Details
#initialize(*args) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'ext/openssl/ossl_bn.c', line 190
static VALUE
ossl_bn_initialize(int argc, VALUE *argv, VALUE self)
{
BIGNUM *bn;
VALUE str, bs;
int base = 10;
if (rb_scan_args(argc, argv, "11", &str, &bs) == 2) {
base = NUM2INT(bs);
}
if (RB_INTEGER_TYPE_P(str)) {
GetBN(self, bn);
integer_to_bnptr(str, bn);
return self;
}
if (RTEST(rb_obj_is_kind_of(str, cBN))) {
BIGNUM *other;
GetBN(self, bn);
GetBN(str, other); /* Safe - we checked kind_of? above */
if (!BN_copy(bn, other)) {
ossl_raise(eBNError, NULL);
}
return self;
}
GetBN(self, bn);
switch (base) {
case 0:
if (!BN_mpi2bn((unsigned char *)StringValuePtr(str), RSTRING_LENINT(str), bn)) {
ossl_raise(eBNError, NULL);
}
break;
case 2:
if (!BN_bin2bn((unsigned char *)StringValuePtr(str), RSTRING_LENINT(str), bn)) {
ossl_raise(eBNError, NULL);
}
break;
case 10:
if (!BN_dec2bn(&bn, StringValueCStr(str))) {
ossl_raise(eBNError, NULL);
}
break;
case 16:
if (!BN_hex2bn(&bn, StringValueCStr(str))) {
ossl_raise(eBNError, NULL);
}
break;
default:
ossl_raise(rb_eArgError, "invalid radix %d", base);
}
return self;
}
|
Class Method Details
.generate_prime ⇒ Object
.pseudo_rand ⇒ Object
BN.pseudo_rand(bits [, fill [, odd]]) -> aBN
.pseudo_rand_range(range) ⇒ Object
.rand ⇒ Object
BN.rand(bits [, fill [, odd]]) -> aBN
.rand_range(range) ⇒ Object
Instance Method Details
#%(bn2) ⇒ Object
#*(bn2) ⇒ Object
#**(bn2) ⇒ Object
#+(bn2) ⇒ Object
#-(bn2) ⇒ Object
#/(bn2) ⇒ Array
Division of OpenSSL::BN instances
#<<(bits) ⇒ Object
#== ⇒ Object Also known as: ===
#>>(bits) ⇒ Object
#bit_set?(bit) ⇒ Boolean
Returns boolean of whether bit
is set. Bitwise operations for openssl BIGNUMs.
#clear_bit!(bit) ⇒ self
#cmp(bn2) ⇒ Integer Also known as: <=>
#coerce(other) ⇒ Object
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'ext/openssl/ossl_bn.c', line 328
static VALUE
ossl_bn_coerce(VALUE self, VALUE other)
{
switch(TYPE(other)) {
case T_STRING:
self = ossl_bn_to_s(0, NULL, self);
break;
case T_FIXNUM:
case T_BIGNUM:
self = ossl_bn_to_i(self);
break;
default:
if (!RTEST(rb_obj_is_kind_of(other, cBN))) {
ossl_raise(rb_eTypeError, "Don't know how to coerce");
}
}
return rb_assoc_new(other, self);
}
|
#copy ⇒ Object
#eql?(obj) ⇒ Boolean
Returns true
only if obj is a OpenSSL::BN
with the same value as big. Contrast this with OpenSSL::BN#==, which performs type conversions.
919 920 921 922 923 924 925 926 927 928 929 930 |
# File 'ext/openssl/ossl_bn.c', line 919
static VALUE
ossl_bn_eql(VALUE self, VALUE other)
{
BIGNUM *bn1, *bn2;
if (!rb_obj_is_kind_of(other, cBN))
return Qfalse;
GetBN(self, bn1);
GetBN(other, bn2);
return BN_cmp(bn1, bn2) ? Qfalse : Qtrue;
}
|
#gcd(bn2) ⇒ Object
#hash ⇒ Integer
Returns a hash code for this object.
See also Object#hash.
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 |
# File 'ext/openssl/ossl_bn.c', line 940
static VALUE
ossl_bn_hash(VALUE self)
{
BIGNUM *bn;
VALUE hash;
unsigned char *buf;
int len;
GetBN(self, bn);
len = BN_num_bytes(bn);
buf = xmalloc(len);
if (BN_bn2bin(bn, buf) != len) {
xfree(buf);
ossl_raise(eBNError, NULL);
}
hash = ST2FIX(rb_memhash(buf, len));
xfree(buf);
return hash;
}
|
#lshift!(bits) ⇒ self
#mask_bits! ⇒ Object
#mod_add(bn1, bn2) ⇒ Object
#mod_exp(bn1, bn2) ⇒ Object
#mod_inverse(bn2) ⇒ Object
#mod_mul(bn1, bn2) ⇒ Object
#mod_sqr(bn2) ⇒ Object
#mod_sub(bn1, bn2) ⇒ Object
#num_bits ⇒ Integer
#num_bytes ⇒ Integer
#odd? ⇒ Boolean
#one? ⇒ Boolean
#pretty_print(q) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/openssl/bn.rb', line 20 def pretty_print(q) q.object_group(self) { q.text ' ' q.text to_i.to_s } end |
#prime? ⇒ Boolean #prime?(checks) ⇒ Boolean
Performs a Miller-Rabin probabilistic primality test with checks
iterations. If nchecks
is not specified, a number of iterations is used that yields a false positive rate of at most 2^-80 for random input.
Parameters
-
checks
- integer
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 |
# File 'ext/openssl/ossl_bn.c', line 974
static VALUE
ossl_bn_is_prime(int argc, VALUE *argv, VALUE self)
{
BIGNUM *bn;
VALUE vchecks;
int checks = BN_prime_checks;
if (rb_scan_args(argc, argv, "01", &vchecks) == 1) {
checks = NUM2INT(vchecks);
}
GetBN(self, bn);
switch (BN_is_prime_ex(bn, checks, ossl_bn_ctx, NULL)) {
case 1:
return Qtrue;
case 0:
return Qfalse;
default:
ossl_raise(eBNError, NULL);
}
/* not reachable */
return Qnil;
}
|
#prime_fasttest? ⇒ Boolean #prime_fasttest?(checks) ⇒ Boolean #prime_fasttest?(checks, trial_div) ⇒ Boolean
Performs a Miller-Rabin primality test. This is same as #prime? except this first attempts trial divisions with some small primes.
Parameters
-
checks
- integer -
trial_div
- boolean
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 |
# File 'ext/openssl/ossl_bn.c', line 1010
static VALUE
ossl_bn_is_prime_fasttest(int argc, VALUE *argv, VALUE self)
{
BIGNUM *bn;
VALUE vchecks, vtrivdiv;
int checks = BN_prime_checks, do_trial_division = 1;
rb_scan_args(argc, argv, "02", &vchecks, &vtrivdiv);
if (!NIL_P(vchecks)) {
checks = NUM2INT(vchecks);
}
GetBN(self, bn);
/* handle true/false */
if (vtrivdiv == Qfalse) {
do_trial_division = 0;
}
switch (BN_is_prime_fasttest_ex(bn, checks, ossl_bn_ctx, do_trial_division, NULL)) {
case 1:
return Qtrue;
case 0:
return Qfalse;
default:
ossl_raise(eBNError, NULL);
}
/* not reachable */
return Qnil;
}
|
#rshift!(bits) ⇒ self
#set_bit!(bit) ⇒ self
#sqr ⇒ Object
#to_bn ⇒ Object
322 323 324 325 326 |
# File 'ext/openssl/ossl_bn.c', line 322
static VALUE
ossl_bn_to_bn(VALUE self)
{
return self;
}
|
#to_i ⇒ Integer Also known as: to_int
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'ext/openssl/ossl_bn.c', line 304
static VALUE
ossl_bn_to_i(VALUE self)
{
BIGNUM *bn;
char *txt;
VALUE num;
GetBN(self, bn);
if (!(txt = BN_bn2hex(bn))) {
ossl_raise(eBNError, NULL);
}
num = rb_cstr_to_inum(txt, 16, Qtrue);
OPENSSL_free(txt);
return num;
}
|
#to_s ⇒ String #to_s(base) ⇒ String
Parameters
-
base
- integer Valid values:-
0 - MPI
-
2 - binary
-
10 - the default
-
16 - hex
-
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'ext/openssl/ossl_bn.c', line 260
static VALUE
ossl_bn_to_s(int argc, VALUE *argv, VALUE self)
{
BIGNUM *bn;
VALUE str, bs;
int base = 10, len;
char *buf;
if (rb_scan_args(argc, argv, "01", &bs) == 1) {
base = NUM2INT(bs);
}
GetBN(self, bn);
switch (base) {
case 0:
len = BN_bn2mpi(bn, NULL);
str = rb_str_new(0, len);
if (BN_bn2mpi(bn, (unsigned char *)RSTRING_PTR(str)) != len)
ossl_raise(eBNError, NULL);
break;
case 2:
len = BN_num_bytes(bn);
str = rb_str_new(0, len);
if (BN_bn2bin(bn, (unsigned char *)RSTRING_PTR(str)) != len)
ossl_raise(eBNError, NULL);
break;
case 10:
if (!(buf = BN_bn2dec(bn))) ossl_raise(eBNError, NULL);
str = ossl_buf2str(buf, rb_long2int(strlen(buf)));
break;
case 16:
if (!(buf = BN_bn2hex(bn))) ossl_raise(eBNError, NULL);
str = ossl_buf2str(buf, rb_long2int(strlen(buf)));
break;
default:
ossl_raise(rb_eArgError, "invalid radix %d", base);
}
return str;
}
|