Ruby 2.4
OpenSSL

OpenSSL::BN

类 OpenSSL :: BN

父类:ObjectIncluded 模块:可比较

公共类方法

generate_prime(bits,[,safe [,add,rem]])→bn 显示源代码

生成一个随机的素数位长度bits。如果safe属实,则生成安全素数。如果add指定,则生成满足条件的素数p % add = rem

参数

  • bits - 整数

  • safe - 布尔值

  • add - BN

  • rem - BN

static VALUE ossl_bn_s_generate_prime(int argc, VALUE *argv, VALUE klass) { BIGNUM *add = NULL, *rem = NULL, *result; int safe = 1, num; VALUE vnum, vsafe, vadd, vrem, obj; rb_scan_args(argc, argv, "13", &vnum, &vsafe, &vadd, &vrem num = NUM2INT(vnum if (vsafe == Qfalse) { safe = 0; } if (!NIL_P(vadd)) { add = GetBNPtr(vadd rem = NIL_P(vrem) ? NULL : GetBNPtr(vrem } obj = NewBN(klass if (!(result = BN_new())) { ossl_raise(eBNError, NULL } if (!BN_generate_prime_ex(result, num, safe, add, rem, NULL)) { BN_free(result ossl_raise(eBNError, NULL } SetBN(obj, result return obj; }

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

构建一个新的 OpenSSL BigNum 对象。

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; }

公共实例方法

bn % bn2 → aBN

bn * bn2 → aBN

bn ** bn2 → aBN

bn + bn2 → aBN

bn - bn2 → aBN

bn1 / bn2→ result, remainder() (结果,余数())

OpenSSL :: BN 实例的划分

static VALUE ossl_bn_div(VALUE self, VALUE other) { BIGNUM *bn1, *bn2 = GetBNPtr(other), *r1, *r2; VALUE klass, obj1, obj2; GetBN(self, bn1 klass = rb_obj_class(self obj1 = NewBN(klass obj2 = NewBN(klass if (!(r1 = BN_new())) { ossl_raise(eBNError, NULL } if (!(r2 = BN_new())) { BN_free(r1 ossl_raise(eBNError, NULL } if (!BN_div(r1, r2, bn1, bn2, ossl_bn_ctx)) { BN_free(r1 BN_free(r2 ossl_raise(eBNError, NULL } SetBN(obj1, r1 SetBN(obj2, r2 return rb_ary_new3(2, obj1, obj2 }

bn << bits → aBN

<=>(p1)

Alias for: cmp

bn == obj→true 或 false 显示来源

仅当obj具有bn相同的值时返回true。与#eql?对比,这需要 obj 为 OpenSSL :: BN。

static VALUE ossl_bn_eq(VALUE self, VALUE other) { BIGNUM *bn1, *bn2; GetBN(self, bn1 other = try_convert_to_bn(other if (NIL_P(other)) return Qfalse; GetBN(other, bn2 if (!BN_cmp(bn1, bn2)) { return Qtrue; } return Qfalse; }

另外别名为:===

===(p1)

别名为:==

bn >> bits → aBN

bit_set?(bit)→true | 假显示源

返回是否bit设置的布尔值。对于 openssl BIGNUMs 的按位操作。

static VALUE ossl_bn_is_bit_set(VALUE self, VALUE bit) { int b; BIGNUM *bn; b = NUM2INT(bit GetBN(self, bn if (BN_is_bit_set(bn, b)) { return Qtrue; } return Qfalse; }

clear_bit!(bit) → self

cmp(bn2)→整数

另外别名为:<=>

coerce(p1)显示源

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(p1) 显示源文件

static VALUE ossl_bn_copy(VALUE self, VALUE other) { BIGNUM *bn1, *bn2; rb_check_frozen(self if (self == other) return self; GetBN(self, bn1 bn2 = GetBNPtr(other if (!BN_copy(bn1, bn2)) { ossl_raise(eBNError, NULL } return self; }

eql?(obj)→true 或 false 显示源文件

仅当 obj 是一个OpenSSL::BNbig 相同的值时才返回true。将其与OpenSSL :: BN#==进行对比,后者执行类型转换。

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) → aBN

hash(散列)→整数显示源文件

返回此对象的哈希码。

另见对象#散列。

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 = INT2FIX(rb_memhash(buf, len) xfree(buf return hash; }

lshift!(bits) → self

mod_add(bn1, bn2) → aBN

mod_exp(bn1, bn2) → aBN

mod_inverse(bn2) → aBN

mod_mul(bn1, bn2) → aBN

mod_sqr(bn2) → aBN

mod_sub(bn1, bn2) → aBN

num_bits → integer

num_bytes → integer

odd? → true | false

one? → true | false

pretty_print(q)显示源文件

# File ext/openssl/lib/openssl/bn.rb, line 19 def pretty_print(q) q.object_group(self) { q.text ' ' q.text to_i.to_s } end

prime?( 主要?)→true | false 显示源

prime?(checks) → true | false

checks迭代执行 Miller-Rabin 概率素性测试。如果nchecks未指定,则使用多次迭代,对于随机输入产生至多2 ^ -80的误报率。

参数

  • checks - 整数值VALUE ossl_bn_is_prime(int argc,VALUE * argv,VALUE self){BIGNUM * bn; VALUE vchecks; int checks = BN_prime_checks; 如果(rb_scan_args(argc,argv,“01”,&vchecks)== 1){checks = NUM​​2INT(vchecks); } GetBN(self,bn); 开关(BN_is_prime_ex(bn,checks,ossl_bn_ctx,NULL)){case 1:return Qtrue; 情况0:返回Qfalse; 默认:ossl_raise(eBNError,NULL); } / *不可达* / return Qnil; } prime_fasttest?→true | false显示源prime_fasttest?(检查)→true | false prime_fasttest?(checks,trial_div)→true | false执行Miller-Rabin素性测试。这和素数一样吗?除了这第一次试图与一些小素数审判师。参数

  • checks - integer

  • trial_div - booleanstatic 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 → aBN to_bn() Show source static VALUE ossl_bn_to_bn(VALUE self) { return self; } to_i → integer Show source 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; } Also aliased as: to_int to_int() Alias for: to_i to_s → string Show source to_s(base) → string Parameters

  • base - 整数有效值:

- 0 - MPI - 2 - binary

- 10 - the default - 16 - hex

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; }

ucmp(bn2) → integer

zero? → true | false